Swift 2.2では、unsafeAddressOf
を使用して、VENDORとPRODUCT IDをUSBマッチング辞書に追加できます。USBマッチング辞書にベンダー/プロダクトIDを追加する方法
var serviceMatchingDictionary = IOServiceMatching(kIOUSBDeviceClassName)
private let VendorID = 0x8564
private let ProductID = 0x5000
let vendorIDString = kUSBVendorID as CFStringRef!
let productIDString = kUSBProductID as CFStringRef!
CFDictionarySetValue(serviceMatchingDictionary, unsafeAddressOf(vendorIDString), unsafeAddressOf(VendorID))
CFDictionarySetValue(serviceMatchingDictionary, unsafeAddressOf(productIDString), unsafeAddressOf(ProductID))
スウィフト3では、私はwithUnsafePointer(to arg: inout T, _ body: (UnsafePointer) throws -> Result) rethrows -> Resultを使用します。
ただし、動作しませんでした。これは、アドレスをプリントアウトすることができますが、それはCFDictionarySetValue
withUnsafePointer(to: &VendorID) { vendorIDPtr in
withUnsafePointer(to: &ProductID, { productIDPtr in
withUnsafePointer(to: &vendorIDString, { vendorIDStringPtr in
withUnsafePointer(to: &productIDString, { productIDStringPtr in
// Thread1: EXC_BAD_ACCESS(code=1, address=0x0)
CFDictionarySetValue(matchingDict, vendorIDStringPtr, vendorIDPtr)
CFDictionarySetValue(matchingDict, productIDStringPtr, productIDPtr)
})
})
})
}
あなたはどのようなエラーメッセージを得るのですか? – mlidal
スレッド1:EXC_BAD_ACCESS(コード= 1、アドレス= 0x0) – WeiJay