FROM : Rick Kitts
DATE : Tue Apr 19 17:33:50 2005
Ah. This is pretty sexy. So categories are 0 runtime cost constructs?
Your example also seems to raise the question of how should one deal
with the delegate. It seems that I could either create a default/do
nothing implementation or I could send a respondsTo: message to the
delegate. I'd presume that the former is faster at runtime, whereas the
second seems to better convey what's actually happening (note: For what
I'm doing there *is* a reasonable default behavior and this is what
raises this issue for me).
Any suggestions?
BTW I think what I'm looking for, ideally, is a sort of best practices
forum or something. Objc is trivial to learn but, like most languages,
difficult to skillfully wield. An Effective Objective-C book would be
really useful.
Thanks again,
---Rick
On Apr 18, 2005, at 9:28 PM, Andrew White wrote:
>
> Rick Kitts wrote:
>> Hello. I'm working on a class that uses a delegate. I was looking at
>> how Apple does this sort of thing and ran into NSXMLParser. It takes
>> a delegate, of course, and the delegate interface is declared as
>> follows:
>> // The parser's delegate is informed of events through the methods in
>> the NSXMLParserDelegateEventAdditions category.
>> @interface NSObject (NSXMLParserDelegateEventAdditions)
>> To be honest, I'm not sure that this means. Does
>> NSXMLParserDelegateEventAdditions
>> define a type that I can subclass?
>
> What the declaration means is that the
> NSXMLParserDelegateEventAdditions category adds the listed methods to
> the NSObject class. Since everything inherits from NSObject, these
> methods are available to all objects in the system. Typically, a
> category will also define default implementations so that objects that
> are unaware of the category don't have problems if called
> unintentionally.
>
>
>> So, I guess what I'm asking is can anyone either a) explain what this
>> stuff is trying to do or b) point me to some docs or a book that can
>> help me understand this sort of thing? I have read the objc manual
>> (PDF) from Apples site and I didn't see this sort of thing covered.
>
> Since these methods are available to all NSObjects (and, by
> inheritance, whatever object you want to be the delegate), all you
> need to do is set your object to be the parser object's delegate and
> then implement (override) the methods specified by the category. You
> don't need to declare anything special, because the category
> declaration has already done that for you.
>
>
> * example:
>
> Declarations and definitions:
>
> @interface NSObject (MyExample)
>
> - (BOOL) dummyExampleMethod;
>
> @end
>
> @implementation NSObject (MyExample)
>
> - (BOOL) dummyExampleMethod
> {
> return NO;
> }
>
> @end
>
>
> @interface MyObject : NSObject
> {
> }
> @end
>
> @implementation MyObject
>
> - (BOOL) dummyExampleMethod
> {
> return YES;
> }
>
> @end
>
>
>
> Code snippet:
> NSString * s = @"String";
> MyObject * m = [[MyObject alloc] init];
>
> [s dummyExampleMethod] will return NO.
> [m dummyExampleMethod] will return YES.
>
> Note that neither the original NSObject nor NSString knew anything
> about dummyExampleMethod - we've added it on later.
>
> -- Andrew White
>
> -----------------------------------------------------------------------
> ---
> This email and any attachments may be confidential. They may contain
> legally
> privileged information or copyright material. You should not read,
> copy,
> use or disclose them without authorisation. If you are not an intended
> recipient, please contact us at once by return email and then delete
> both
> messages. We do not accept liability in connection with computer virus,
> data corruption, delay, interruption, unauthorised access or
> unauthorised
> amendment. This notice should not be removed.
>
DATE : Tue Apr 19 17:33:50 2005
Ah. This is pretty sexy. So categories are 0 runtime cost constructs?
Your example also seems to raise the question of how should one deal
with the delegate. It seems that I could either create a default/do
nothing implementation or I could send a respondsTo: message to the
delegate. I'd presume that the former is faster at runtime, whereas the
second seems to better convey what's actually happening (note: For what
I'm doing there *is* a reasonable default behavior and this is what
raises this issue for me).
Any suggestions?
BTW I think what I'm looking for, ideally, is a sort of best practices
forum or something. Objc is trivial to learn but, like most languages,
difficult to skillfully wield. An Effective Objective-C book would be
really useful.
Thanks again,
---Rick
On Apr 18, 2005, at 9:28 PM, Andrew White wrote:
>
> Rick Kitts wrote:
>> Hello. I'm working on a class that uses a delegate. I was looking at
>> how Apple does this sort of thing and ran into NSXMLParser. It takes
>> a delegate, of course, and the delegate interface is declared as
>> follows:
>> // The parser's delegate is informed of events through the methods in
>> the NSXMLParserDelegateEventAdditions category.
>> @interface NSObject (NSXMLParserDelegateEventAdditions)
>> To be honest, I'm not sure that this means. Does
>> NSXMLParserDelegateEventAdditions
>> define a type that I can subclass?
>
> What the declaration means is that the
> NSXMLParserDelegateEventAdditions category adds the listed methods to
> the NSObject class. Since everything inherits from NSObject, these
> methods are available to all objects in the system. Typically, a
> category will also define default implementations so that objects that
> are unaware of the category don't have problems if called
> unintentionally.
>
>
>> So, I guess what I'm asking is can anyone either a) explain what this
>> stuff is trying to do or b) point me to some docs or a book that can
>> help me understand this sort of thing? I have read the objc manual
>> (PDF) from Apples site and I didn't see this sort of thing covered.
>
> Since these methods are available to all NSObjects (and, by
> inheritance, whatever object you want to be the delegate), all you
> need to do is set your object to be the parser object's delegate and
> then implement (override) the methods specified by the category. You
> don't need to declare anything special, because the category
> declaration has already done that for you.
>
>
> * example:
>
> Declarations and definitions:
>
> @interface NSObject (MyExample)
>
> - (BOOL) dummyExampleMethod;
>
> @end
>
> @implementation NSObject (MyExample)
>
> - (BOOL) dummyExampleMethod
> {
> return NO;
> }
>
> @end
>
>
> @interface MyObject : NSObject
> {
> }
> @end
>
> @implementation MyObject
>
> - (BOOL) dummyExampleMethod
> {
> return YES;
> }
>
> @end
>
>
>
> Code snippet:
> NSString * s = @"String";
> MyObject * m = [[MyObject alloc] init];
>
> [s dummyExampleMethod] will return NO.
> [m dummyExampleMethod] will return YES.
>
> Note that neither the original NSObject nor NSString knew anything
> about dummyExampleMethod - we've added it on later.
>
> -- Andrew White
>
> -----------------------------------------------------------------------
> ---
> This email and any attachments may be confidential. They may contain
> legally
> privileged information or copyright material. You should not read,
> copy,
> use or disclose them without authorisation. If you are not an intended
> recipient, please contact us at once by return email and then delete
> both
> messages. We do not accept liability in connection with computer virus,
> data corruption, delay, interruption, unauthorised access or
> unauthorised
> amendment. This notice should not be removed.
>






Cocoa mail archive

