Re: OBJ-C question
Re: OBJ-C question
- Subject: Re: OBJ-C question
- From: "Louis C. Sacha" <email@hidden>
- Date: Fri, 16 Apr 2004 20:07:02 -0700
Hello...
Well, you need to make sure you have imported the header file (where
the methods of your informal protocol are declared) in any code where
you use them.
Normally, informal protocols are implemented as a category of
NSObject (or if it should be limited to objects of a certain type,
then in the common "super" class for that type).
- - - - - YourInformalProtocol.h - - - - -
@interface NSObject (YourInformalProtocol)
- (void)doSomething:(id)sender;
- (NSString *)importantInfo;
- (void)setImportantInfo:(NSString *)someInfo;
@end
- - - - - - - - - -
Then in any class that needs to deal with objects that should respond
to your informal protocol:
- - - - - YourOtherClass.h - - - - -
#import "SomeSuperClass.m"
@interface YourOtherClass: SomeSuperClass
{
id informalProtocolUsingObject;
/* ... */
}
- (NSString *)infoFromObject;
/* ... */
@end
- - - - - - - - - -
- - - - - YourOtherClass.m - - - - -
#import "YourOtherClass.m"
#import "YourInformalProtocol.h"
@implementation YourOtherClass
- (NSString)infoFromObject
{
return [informalProtocolUsingObject importantInfo];
}
/* ... */
@end
- - - - - - - - - -
If implementing a method in your informal protocol is optional, you
would check if the object supports the method first:
if ([informalProtocolUsingObject
respondsToSelector:@selector(importantInfo)]) {return
[informalProtocolUsingObject importantInfo];}
else {return nil;}
Hope that helps,
Louis
Not quite on topic, but here's the question:
If I have an informal protocol, and am sending a message conforming
to the protocol to an object of type id, is it normal to have
warning messages appear during builds? Is there a way to tell the
compiler to "trust me", like you can do when typecasting variables?
Or do you just live with the build warning messages.
Mike
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.