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: Eric Gorr <email@hidden>
- Date: Wed, 18 Nov 2015 08:24:39 -0500
Yes, that all makes sense. So, the question is what can I do about it? The number of tooltips I need is the same number of elements in the array and the number of elements in the array can change over the lifetime of the application. So, it is natural to store the information needed by the tooltip inside of an array and pass a pointer to an array element as the userdata for the tooltip. If my current implementation is not going to work, what is the recommended implementation for this kind of behavior?
I do have this simple test project at: https://github.com/ericgorr/tooltip_arrays <https://github.com/ericgorr/tooltip_arrays>
> On Nov 17, 2015, at 9:48 PM, Jens Alfke <email@hidden> wrote:
>
> I think the cause is “&(myArray[0])”. In Swift it isn't possible to get a pointer to an array item — these aren’t C arrays, their internal representation is opaque! So what I think happens is that it copies myArray[0] into a temporary and creates an UnsafeMutablePointer to that. Then, after the call, it assumes that the call might have mutated that temporary, so it stores it back into myArray.
>
> Then, mutating myArray is logically equivalent to copying it, modifying the copy, and storing the copy back into the property. (Apparently it’s often optimized, fortunately, but those are the semantics.) Which means that your ‘didSet’ handler gets called.
>
> —Jens
_______________________________________________
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