为什么 NSMutableData 改变指针的地址?

Why NSMutableData change address of pointer?

我想快速预分配内存并用 NSMutableData 包装它,但可以通过指针访问。 所以我有这个:

var vertex = UnsafeMutablePointer<Float>.alloc(numberOfVertex * 3)
vertex.initialize(0)
vertexData = NSMutableData(bytesNoCopy: vertex, length: numberOfVertex * 3 * sizeof(Float))

但是当我检查 vertex 指针的地址时,我看到了与 vertexData.bytes 不同的值,因此据我所知,我不能再使用指针按索引更改数据。 如何避免这种情况并且只有一个内存块?

根据文档,您实际上不能将 NSMutableData 与 no-copy 标志一起使用:

NSMutableData responds to these methods, too, but the bytes are copied anyway and the buffer is freed immediately.

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/BinaryData/Tasks/WorkingBinaryData.html#//apple_ref/doc/uid/20000717-149014