Re: Assigning an element of a swift array to a userdata parameter triggers didSet
Re: Assigning an element of a swift array to a userdata parameter triggers didSet
- Subject: Re: Assigning an element of a swift array to a userdata parameter triggers didSet
- From: John McCall <email@hidden>
- Date: Wed, 18 Nov 2015 11:52:42 -0800
> On Nov 18, 2015, at 11:12 AM, Quincey Morris <email@hidden> wrote:
> On Nov 18, 2015, at 10:57 , Jens Alfke <email@hidden> wrote:
>>
>> Doesn’t the pointer become invalid as soon as myElement goes out of scope? (There’s a reason that type is called *Unsafe*Pointer…)
>
> Yes, but so does &myArray[0] in the original code. According to your hypothesis, it’s copied into a temporary, which also becomes invalid as soon as the current scope ends.
It’s actually shorter than that; inout temporaries are valid just for the duration of the call.
Going back to your actual problem: “user data” parameters like this are really just pointer-sized opaque buckets of bits. Assuming your array is, in fact, stably-indexed (and if it isn’t, passing a pointer won’t work either!), consider just passing an index as the user data; you can construct a pointer from an Int like this:
UnsafePointer(bitPattern: i)
and get the Int back like this:
unsafeBitCast(ptr, Int.self)
The fact that the latter is apparently the only interface for getting back to an Int is probably a bug.
John.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden