Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]
Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]
- Subject: Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]
- From: Conrad Shultz <email@hidden>
- Date: Mon, 25 Mar 2013 19:33:00 -0700
On Mar 25, 2013, at 4:06 PM, Chris Tracewell <email@hidden> wrote:
> On Mar 25, 2013, at 1:54 PM, Conrad Shultz <email@hidden> wrote:
>> "[self delegate]" is not the same as "delegate" - the former sends the -delegate message, the latter references a variable named "delegate" (which the compiler is telling you does not exist).
>
> I assumed that since my subclass inherits from NSOutlineView which already has a delegate instance method that a delegate ivar already existed? Since this is occuring while converting to ARC could it be that _delegate is working because modern Objective-c adds the underscore to the front of ivars during synth? Still though, I do not understand why [self delegate] will not work.
Unless a class is publicly documented as having an ivar you should access (and I can't think of any examples offhand!), you must always use the documented methods (or properties, which are really just methods). Even if there were an ivar named "delegate" that would be an internal implementation detail subject to change at any time. -[NSOutlineView delegate] is the documented public method to access an NSOutlineView's delegate.
(It is worth noting that there is no requirement whatsoever that a property even be backed by an ivar!)
>
> this must be that my delegate declarations are incorrect.
If you are working with an NSOutlineView or subclass thereof, you don't declare the delegate; NSOutlineView itself does:
delegate
Returns the receiver’s delegate.
- (id < NSOutlineViewDelegate >)delegate
Return Value
The receiver’s delegate.
Availability
Available in OS X v10.6 and later.
See Also
– setDelegate:
Declared In
NSOutlineView.h
>
>> For example, here you correctly use "[self delegate]" in the if() condition but improperly fall back to merely "delegate" for actually dispatching your delegate message.
>
> I had replaced [self delegate] with "delegate" while troubleshooting, but both trigger the error. I am transitioning to ARC, [self delegate] was working compiling in XCode 3.2 for 10.6.8.
In the code you shared you had used "delegate" in one place and "[self delegate]" in another; the second case is the correct one. If you replace all naked uses of "delegate" with "[self delegate]" and you continue to get compile-time errors please post your entire class or a reduced test case.
Oh, I just took another look at your code. It looks like you are declaring your own supplemental delegate methods. It's fine to have the outline view's delegate also handle your custom delegate methods, but you might consider just declaring your own -[TKOutlineView customDelegate] or the like that points to said object. This would allow you to get compile-time checks (since you could declare customDelegate as requiring conformance to your protocol) and might make your code more maintainable since you could more easily separate out your own delegate methods from the NSOutlineViewDelegate methods when your situation becomes more complex.
Good luck!
-Conrad
_______________________________________________
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