1
float *vertexBuffer = (float *)positionSource.data.bytes;

'bytes' 不可用:改用 withUnsafeBytes

但我不知道如何使用它

_ = positionSource?.data.withUnsafeBytes({ (<#UnsafePointer<ContentType>#>) -> ResultType in

   })
4

2 回答 2

1

我认为你可以这样做。

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
}

希望能帮到你!

于 2016-11-25T10:51:17.710 回答
0

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
}
于 2016-11-25T09:19:18.050 回答