Re: Category or Protocol? (objc newbie)
Re: Category or Protocol? (objc newbie)
- Subject: Re: Category or Protocol? (objc newbie)
- From: Andrew White <email@hidden>
- Date: Tue, 19 Apr 2005 14:28:38 +1000
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.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden