• 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: Charles Srstka <email@hidden>
  • Date: Tue, 22 Jan 2013 13:19:45 -0600

On Jan 22, 2013, at 12:58 PM, Andy Lee <email@hidden> wrote:

> // Or this also works (protocol).
> @protocol AvoidCompilerWarning
> - (id)initWithArg:(id)arg;
> @end

Really, a protocol is what you ought to be doing. Make a protocol with -initWithManager: in it, and then make all the classes that might get passed to this method comply with your protocol. Then, do this:

if ([myClass conformsToProtocol:@protocol(MyProtocol)])
	myObj = [[myClass alloc] initWithManager:sel]];
else
	myObj = [[myClass alloc] init];

The advantage to this method is a simple one: Suppose some random class happens to implement a method named -initWithManager:, but that method has nothing to do with your -initWithManager: other than a coincidental title, and takes a completely different type of object. Your original code will result in unpredictable behavior in this case (and probably throw an exception leading to a crash). If you use a protocol, you'll know not just that the method responds to something named -initWithManager:, but that it's *your* -initWithManager:

Charles

_______________________________________________

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>)

  • Prev by Date: Re: How to avoid warning?
  • Next by Date: Re: object.struct.element as lvalue
  • Previous by thread: Re: How to avoid warning?
  • Next by thread: Re: How to avoid warning?
  • Index(es):
    • Date
    • Thread