0

I want to quick preallocate memory and have it wrapped by NSMutableData but with access via pointer. So I have this:

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

But when I check address of vertex pointer I see different value then vertexData.bytes so as I understand I can't use pointer anymore to change data by index. How to avoid this and have only one memory block?

John Tracid
  • 3,836
  • 3
  • 22
  • 33
  • This is more an issue of the Foundation framework, independent of the used language (Swift or Objective-C). – Martin R Sep 14 '15 at 21:36
  • @MartinR should I remove question since it is a duplicate? – John Tracid Sep 14 '15 at 22:01
  • I don't think there is a need to remove the question, duplicates are not generally a bad thing. It helps other people coming from Swift to find the answer. – Martin R Sep 14 '15 at 22:04

1 Answers1

2

According to the documentation, you cannot use NSMutableData with the no-copy flag that way actually:

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

iOSX
  • 1,270
  • 13
  • 18