如何将"any"的类型转换为"UnsafeMutablePointer<Int16>"的类型swift 3
How to convert a type of "any" in kind "UnsafeMutablePointer<Int16>" in a swift 3
我更新了我的 xcode 以获取最新的测试版,但出现了这样的问题。
在我的代码在 objective-c:
中运行良好之前
short * pointers = (short *)[[params valueForKey:@"Pointers"] pointerValue];
然后我翻译了swift语言的部分代码:
let pointers = UnsafeMutablePointer<Int16>(((params["Pointers"] as AnyObject).pointerValue)!)
这里也一样,一切都很好。 But this code is not working on the Swift 3。告诉你如何实现 swift 3. 谢谢!
将指针转换为包含在 NSValue
中的 UInt16
的正确方法
UnsafeMutablePointer<Int16>
在 Swift 3 是
if let ptr = value.pointerValue {
let u16ptr = ptr.assumingMemoryBound(to: UInt16.self)
// ...
} else {
// pointer is nil
}
你的情况应该是
if let ptr = (params["Pointers"] as? NSValue)?.pointerValue {
let u16ptr = ptr.assumingMemoryBound(to: UInt16.self)
// ...
} else {
// value does not exist, is not an `NSValue`, or pointer is nil
}
我更新了我的 xcode 以获取最新的测试版,但出现了这样的问题。 在我的代码在 objective-c:
中运行良好之前short * pointers = (short *)[[params valueForKey:@"Pointers"] pointerValue];
然后我翻译了swift语言的部分代码:
let pointers = UnsafeMutablePointer<Int16>(((params["Pointers"] as AnyObject).pointerValue)!)
这里也一样,一切都很好。 But this code is not working on the Swift 3。告诉你如何实现 swift 3. 谢谢!
将指针转换为包含在 NSValue
中的 UInt16
的正确方法
UnsafeMutablePointer<Int16>
在 Swift 3 是
if let ptr = value.pointerValue {
let u16ptr = ptr.assumingMemoryBound(to: UInt16.self)
// ...
} else {
// pointer is nil
}
你的情况应该是
if let ptr = (params["Pointers"] as? NSValue)?.pointerValue {
let u16ptr = ptr.assumingMemoryBound(to: UInt16.self)
// ...
} else {
// value does not exist, is not an `NSValue`, or pointer is nil
}