float *vertexBuffer = (float *)positionSource.data.bytes;
'bytes' 不可用:改用 withUnsafeBytes
但我不知道如何使用它
_ = positionSource?.data.withUnsafeBytes({ (<#UnsafePointer<ContentType>#>) -> ResultType in
})
float *vertexBuffer = (float *)positionSource.data.bytes;
'bytes' 不可用:改用 withUnsafeBytes
但我不知道如何使用它
_ = positionSource?.data.withUnsafeBytes({ (<#UnsafePointer<ContentType>#>) -> ResultType in
})
我认为你可以这样做。
let str = "hello"
let strData = str.data(using: String.Encoding.utf8)!
strData.withUnsafeBytes { (bytes: UnsafePointer<CChar>) -> Void in
print("\(bytes[1])") //exp: access as c string
}
希望能帮到你!
withUnsafeBytes
是一个泛型方法,ContentType
是从闭包的类型推断出来的。和
data.withUnsafeBytes { (vertexBuffer: UnsafePointer<Float>) in
// Use vertexBuffer ...
}
你会得到一个UnsafePointer<Float>
指向数据中的字节。请注意,此指针不得在闭包之外使用。
您还可以计算结果并将其从闭包传回给调用者。例子:
let result = data.withUnsafeBytes { (vertexBuffer: UnsafePointer<Float>) -> Float in
let result = // ... compute result ...
return result
}