[swift-users] NSData and UnsafePointer

J.E. Schotsman jeschot at xs4all.nl
Sat Jul 16 07:28:28 CDT 2016


A mysterious bug has got me thinking about using UnsafePointer<CChar> with NSData (Swift 2).

Is this safe:

let data:NSData = …
let dataStart = UnsafePointer<CChar>(data:NSDAta.bytes)

myProcessdata1(dataStart,data.length)

… (no more references to data)

And this:

let data:NSData = …
myProcessdata2(data)

… (no more references to data)

func myProcessdata2( data:NSData )
{
let dataStart = UnsafePointer<CChar>(data:NSData.bytes)
myProcessdata1(dataStart,data.length)
}

In the latter case I would hope that data remains alive until the function myProcessdata2 returns. But does it?

TIA,

Jan E.




More information about the swift-users mailing list