Wow! Good stuff Michael :-) I can't believe I never noticed that.
On Thu, Apr 8, 2010 at 3:07 PM, Michael Ash <michael....@gmail.com> wrote: > On Thu, Apr 8, 2010 at 1:21 PM, Patrick M. Rutkowski <rutsk...@gmail.com> > wrote: >> Agreed, but there's always the danger the +array method, which might >> actually be implemented in NSArray.m, will not properly initialize the >> more specific NSMutableArray object. >> >> Of course, in this specific case that's the case, but it might be the >> case with other class hierarchies. >> >> But, nonetheless I'm troubled that nobody in this thread has >> acknowledged that yet :-o > > Take a look at the declaration for this method: > > + (id)array; > > This is subtle... but it actually tells you everything you need to > know for this question. > > The key is the "id" return type. Although at the language level this > just means that it returns some kind of object, it has a deeper > meaning when it comes to Cocoa conventions. More specifically, an "id" > type on a factory method like this means that the method will return > an instance of the class that the message is sent to, even if it's a > subclass. (It could return an instance of a subclass of that class, > but that's perfectly legal.) The "id' means that subclasses will work > correctly, and that the implementation uses [self alloc]. > > Virtually all factory methods are declared like this, and thus that's > how they work. > > As a counterexample, look at +[NSParagraphStyle defaultParagraphStyle]: > > + (NSParagraphStyle *)defaultParagraphStyle; > > It's not declared to return 'id'. This means that > [NSMutableParagraphStyle defaultParagraphStyle] is *not* guaranteed to > return an instance of NSMutableParagraphStyle. It could, but you > shouldn't count on it. You have to assume that the object you get is > always a straight NSParagraphStyle, and code accordingly. (In this > particular case, you'd make a mutable copy of the returned object, > then use that.) > > As a bonus, this convention means that the compiler will yell at you > if you try to write code like: > > NSMutableParagraphStyle *style = [NSMutableParagraphStyle > defaultParagraphStyle]; > > Of course you can't count on warnings to keep you safe all the time, > but it helps. > > So check for the 'id' return value, and if it's there, you can code in > confidence. > > Mike > _______________________________________________ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > 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: > http://lists.apple.com/mailman/options/cocoa-dev/rutski89%40gmail.com > > This email sent to rutsk...@gmail.com > _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com