更新到 Swift 3 后,我的代码有点问题。在转换之前我有这个代码:
extension NSData {
func castToCPointer<T>() -> T {
let mem = UnsafeMutablePointer<T>.alloc(sizeof(T.Type))
self.getBytes(mem, length: sizeof(T.Type))
return mem.move()
}
}
我把它转换成这个代码,在第三行我得到一个错误
...无法将“UnsafeMutablePointer”类型的值转换为预期的参数类型“UnsafeMutablePointer”
extension Data {
func castToCPointer<T>() -> T{
let mem = UnsafeMutablePointer<T>.allocate(capacity: MemoryLayout<T.Type>.size)
self.copyBytes(to: mem, count: MemoryLayout<T.Type>.size)
//self.copyBytes(to: mem, count: MemoryLayout<T.Type>.size)
return mem.move()
}
}
有谁知道如何摆脱这个?