Swift: 无法将类型 'UnsafeMutablePointer' 的值转换为预期的参数类型 'UnsafeMutablePointer'
Swift: Cannot convert value of type 'UnsafeMutablePointer' to expected argument type 'UnsafeMutablePointer'
更新到 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()
}
}
然后我将其转换为此代码,在第 3 行出现错误
... Cannot convert value of type 'UnsafeMutablePointer' to expected argument type '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()
}
}
有谁知道如何摆脱这个?
copyBytes
期望 UnsafeMutableBufferPointer
作为参数:
extension Data {
func castToCPointer<T>() -> T {
let mem = UnsafeMutablePointer<T>.allocate(capacity: 1)
_ = self.copyBytes(to: UnsafeMutableBufferPointer(start: mem, count: 1))
return mem.move()
}
}
(allocate()
将 "items" 的个数作为参数,而不是
字节。)
但是注意你的方法会泄漏内存,分配的内存
被取消初始化(move()
)但也必须是
释放:
extension Data {
func castToCPointer<T>() -> T {
let mem = UnsafeMutablePointer<T>.allocate(capacity: 1)
_ = self.copyBytes(to: UnsafeMutableBufferPointer(start: mem, count: 1))
let val = mem.move()
mem.deallocate(capacity: 1)
return val
}
}
一个更简单的解决方案是(来自
):
extension Data {
func castToCPointer<T>() -> T {
return self.withUnsafeBytes { [=12=].pointee }
}
}
您还可以在扩展中使用以下语法
extension Data {
func castToCPointer<T>() -> T {
var bytes = self.bytes
var val = withUnsafePointer(to: &bytes) { (temp) in
return unsafeBitCast(temp, to: T.self)
}
return val
}
}
var data:NSData/NSMutableData
var bytes = data.bytes
var val = withUnsafePointer(to: &bytes) { (temp) in
return unsafeBitCast(temp, to: T.self)
}
更新到 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()
}
}
然后我将其转换为此代码,在第 3 行出现错误
... Cannot convert value of type 'UnsafeMutablePointer' to expected argument type '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()
}
}
有谁知道如何摆脱这个?
copyBytes
期望 UnsafeMutableBufferPointer
作为参数:
extension Data {
func castToCPointer<T>() -> T {
let mem = UnsafeMutablePointer<T>.allocate(capacity: 1)
_ = self.copyBytes(to: UnsafeMutableBufferPointer(start: mem, count: 1))
return mem.move()
}
}
(allocate()
将 "items" 的个数作为参数,而不是
字节。)
但是注意你的方法会泄漏内存,分配的内存
被取消初始化(move()
)但也必须是
释放:
extension Data {
func castToCPointer<T>() -> T {
let mem = UnsafeMutablePointer<T>.allocate(capacity: 1)
_ = self.copyBytes(to: UnsafeMutableBufferPointer(start: mem, count: 1))
let val = mem.move()
mem.deallocate(capacity: 1)
return val
}
}
一个更简单的解决方案是(来自
extension Data {
func castToCPointer<T>() -> T {
return self.withUnsafeBytes { [=12=].pointee }
}
}
您还可以在扩展中使用以下语法
extension Data {
func castToCPointer<T>() -> T {
var bytes = self.bytes
var val = withUnsafePointer(to: &bytes) { (temp) in
return unsafeBitCast(temp, to: T.self)
}
return val
}
}
var data:NSData/NSMutableData
var bytes = data.bytes
var val = withUnsafePointer(to: &bytes) { (temp) in
return unsafeBitCast(temp, to: T.self)
}