• 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: NSObject Exercise comments?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSObject Exercise comments?


  • Subject: Re: NSObject Exercise comments?
  • From: Ondra Cada <email@hidden>
  • Date: Sat, 10 Dec 2005 17:54:00 +0100

Lee,

On 10.12.2005, at 17:34, Lee Cullens wrote:

Initializers should return an id.
- (id)initWithNumerator:(int)numerator denominator:(int) denominator;

I don't understand why one would use an indeterminate object reference type unless there was intended polymorphism afoot, or in the processing code a variable was being used for different objects (which doesn't help readability) .

It's just a convention, given the ObjC flexibility, not too important. If you insist, freely use any object type anywhere. Your programs will work all right. In a majority of cases though you'll find there was a reason for the convention :)

Here, the idea is that init methods are indeed polymorphic (vast majority of them, at least), and also they are to be overridden in a subclass; unless you use generic object type return, you would have to cast there:

@interface A:NSObject
-(A*)init;
@end
@interface B:A @end
@implementation B
-(B*)init {
// either add a cast or you'll get a warning:
if (!(self=[super init])) return nil;
...

What's worse, with polymorphic inits you'll have to cast or get bunch of warnings in all those [[X alloc] init] expressions. The result would be as ugly code as in the Java rot, ick.

Of course, in case your init is very specific and never overridden, it's uniportant: in that case the only reason to use id return is consistency with all the other polymorhic inits :)
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
References: 
 >Re: NSObject Exercise comments? (From: Lee Cullens <email@hidden>)

  • Prev by Date: Re: NSObject Exercise comments?
  • Next by Date: Re: Audio & Video Capture
  • Previous by thread: Re: NSObject Exercise comments?
  • Next by thread: Re: NSObject Exercise comments?
  • Index(es):
    • Date
    • Thread