Skip navigation.
 
mlRe: Category or Protocol? (objc newbie)
FROM : Andrew White
DATE : Tue Apr 19 06:28:38 2005

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.

Related mailsAuthorDate
mlCategory or Protocol? (objc newbie) Rick Kitts Apr 19, 04:36
mlRe: Category or Protocol? (objc newbie) Andrew White Apr 19, 06:28
mlRe: Category or Protocol? (objc newbie) Nathan Day Apr 19, 06:31
mlRe: Category or Protocol? (objc newbie) Rick Kitts Apr 19, 17:33
mlRe: Category or Protocol? (objc newbie) Ondra Cada Apr 19, 18:52
mlRe: Category or Protocol? (objc newbie) Rick Kitts Apr 19, 21:40
mlRe: Category or Protocol? (objc newbie) Ondra Cada Apr 19, 21:56
mlRe: Category or Protocol? (objc newbie) Charilaos Skiadas Apr 19, 22:04
mlRe: Category or Protocol? (objc newbie) John Brownlow Apr 19, 22:11
mlEffective Cocoa (was Category or Protocol? (objc newbie)) Keith Alperin Apr 19, 22:13
mlRe: Effective Cocoa (was Category or Protocol? (objc newbie)) Rick Kitts Apr 19, 22:21
mlRe: Effective Cocoa (was Category or Protocol? (objc newbie)) Shawn Erickson Apr 19, 22:21
mlRe: Effective Cocoa (was Category or Protocol? (objc newbie)) Finlay Dobbie Apr 19, 22:54
mlRe: Effective Cocoa (was Category or Protocol? (objc newbie)) Ondra Cada Apr 19, 23:44
mlRe: Category or Protocol? (objc newbie) Ondra Cada Apr 19, 23:47