• 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: How to avoid warning?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to avoid warning?


  • Subject: Re: How to avoid warning?
  • From: Uli Kusterer <email@hidden>
  • Date: Wed, 23 Jan 2013 14:22:19 +0100

On Jan 22, 2013, at 8:28 PM, Andy Lee <email@hidden> wrote:
> To be extra fail-safe, you might want to perform a cast to be sure the right initWithManager: gets called:
>
> if ([myClass conformsToProtocol:@protocol(MyProtocol)])
> 	myObj = [(id <MyProtocol>)[myClass alloc] initWithManager:self];
> else
> 	myObj = [[myClass alloc] init];


 This only works for classes that actually declare themselves to be in conformance with this protocol, though. Even if a class implements all the methods in a protocol, conformsToProtocol: will return NO if it doesn't have the protocol name on the @interface (or on the @implementation line).

IMO the nicest way to do this (if you can't make all your classes conform to the protocol by declaring them as @interface Foo : NSObject <UKCanInitWithManagerProtocol>, which would definitely be the cleanest approach), would be:

@protocol UKCanInitWithManagerProtocol
-(id)	initWithManager: (Foo*)inManager;
@end

...

if( [myClass respondsToSelector: @selector(initWithManager:)] )
	myObj = [(id<UKCanInitWithManagerProtocol>)[myClass alloc] initWithManager: self];
else
	myObj = [[myClass alloc] init];

Though I would recommend choosing a different name. E.g. -initWithBarManager: if the manager class is called UKBarManager. "Manager" alone is too generic a word, and someone might have declared it in ObjC++ and taking a C++ object and then you're screwed.

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: How to avoid warning?
      • From: Andy Lee <email@hidden>
References: 
 >How to avoid warning? (From: Dave <email@hidden>)
 >Re: How to avoid warning? (From: Jens Alfke <email@hidden>)
 >Re: How to avoid warning? (From: Dave <email@hidden>)
 >Re: How to avoid warning? (From: Jens Alfke <email@hidden>)
 >Re: How to avoid warning? (From: Andy Lee <email@hidden>)
 >Re: How to avoid warning? (From: Charles Srstka <email@hidden>)
 >Re: How to avoid warning? (From: Andy Lee <email@hidden>)

  • Prev by Date: Re: How to avoid warning?
  • Next by Date: Re: Best guess at expressing a string as a variable
  • Previous by thread: Re: How to avoid warning?
  • Next by thread: Re: How to avoid warning?
  • Index(es):
    • Date
    • Thread