• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: OBJ-C question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.


  • Prev by Date: Re: [now OT] Licensing/Implementing in Cocoa/Obj-C - Interest in product?
  • Next by Date: Re: Removing *other* app default?!?
  • Previous by thread: Re: OBJ-C question
  • Next by thread: Help with Connect to Telnet Server code
  • Index(es):
    • Date
    • Thread