【发布时间】:2017-05-23 10:21:41
【问题描述】:
在 Swift 2.2 中,我可以使用 unsafeAddressOf 将 VENDOR 和 PRODUCT ID 添加到 USB 匹配字典中。
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))
在 Swift 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)
})
})
})
}
【问题讨论】:
-
你得到什么错误信息?
-
线程 1:EXC_BAD_ACCESS(code=1, address=0x0)