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: Jens Alfke <email@hidden>
- Date: Tue, 17 Nov 2015 18:48:32 -0800
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