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: Quincey Morris <email@hidden>
- Date: Wed, 18 Nov 2015 10:17:36 -0800
- Feedback-id: 167118m:167118agrif8a:167118spuspKQuYv:SMTPCORP
On Nov 18, 2015, at 05:24 , Eric Gorr <email@hidden> wrote:
>
> 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?
If the array element (a dictionary, in your original post) is small enough, you can pass a pointer to a copy of the element instead of a pointer to the element. That is:
var myElement = myArray[0]
view.addToolTipRect( NSMakeRect( 0, 0, 100, 100), owner: self, userData: &myElement )
If you don’t want to make a copy of the element, the best approach is to make the array elements reference types rather than value types. (You could use NSDictionary, for example.) Swift makes it so easy to use value types, it’s hard to remember that the old Obj-C way of doing things is sometimes better.
You should probably also submit a bug report about the NSView tool tip APIs. There ought to be a better way in Swift than using unmanaged memory pointers. They have potential memory management issues for ARC, too.
_______________________________________________
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