• 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: Julien Dufour <email@hidden>
  • Date: Sat, 17 Apr 2004 17:52:51 +0200

On Apr 17, 2004, at 13:41, Ondra Cada wrote:

How messy. I think I will stick to formal protocols :)

Pretty difficult to specify a method which a class may but need not to
implement, though ;)

What about avoiding adapting the protocol? If a class does not implement the protocol, it just should not mention it in its interface. NSCopying works that way.

Then, you can stick with the informal oriented test:

id target = ...;
if ([target respondsToSelector:@selector(foo)]) {
[target foo];
}

... or the formal oriented one:

id target = ...;
if ([[target class] conformsToProtocol:@protocol(FormalProtocol)]) {
[target foo];
}

You can then even ask a little more from the compiler by typing the target so that your call are checked too:

id target = ...;
if ([[target class] conformsToProtocol:@protocol(FormalProtocol)]) {
id <FormalProtocol> formalTarget = target;
[formalTarget foo];
}


The only difference I can see is that with the formal approach, if you adapt the protocol but forget to implement the "foo" method or make a typo, the compiler will warn you. With the informal approach, you may notice the error by chance when running the program and you will lose some time tracking it...

I don't want to go into another static vs dynamic fight again, but for once ObjC lets the compiler help you a little, it may be a good idea to take advantage from it. Both methods are working, so the choice is up to everyone.

Best regards.

Julien Dufour
Inferiis - http://www.inferiis.com
_______________________________________________
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.


References: 
 >OBJ-C question (From: "Mike R. Manzano" <email@hidden>)
 >Re: OBJ-C question (From: Ondra Cada <email@hidden>)
 >Re: OBJ-C question (From: "Mike R. Manzano" <email@hidden>)
 >Re: OBJ-C question (From: Ondra Cada <email@hidden>)

  • Prev by Date: Re: Cocoa vs User: 0 - 1
  • Next by Date: Re: Cocoa vs User: 0 - 1
  • Previous by thread: Re: OBJ-C question
  • Next by thread: Re: OBJ-C question
  • Index(es):
    • Date
    • Thread