• 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: [NSMutableArray array]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [NSMutableArray array]


  • Subject: Re: [NSMutableArray array]
  • From: "Patrick M. Rutkowski" <email@hidden>
  • Date: Thu, 8 Apr 2010 15:56:17 -0400

Wow! Good stuff Michael :-)

I can't believe I never noticed that.

On Thu, Apr 8, 2010 at 3:07 PM, Michael Ash <email@hidden> wrote:
> On Thu, Apr 8, 2010 at 1:21 PM, Patrick M. Rutkowski <email@hidden> 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 (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
>
_______________________________________________

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

References: 
 >[NSMutableArray array] (From: "Patrick M. Rutkowski" <email@hidden>)
 >Re: [NSMutableArray array] (From: Dave DeLong <email@hidden>)
 >Re: [NSMutableArray array] (From: "Patrick M. Rutkowski" <email@hidden>)
 >Re: [NSMutableArray array] (From: Bill Bumgarner <email@hidden>)
 >Re: [NSMutableArray array] (From: "Patrick M. Rutkowski" <email@hidden>)
 >Re: [NSMutableArray array] (From: Michael Ash <email@hidden>)

  • Prev by Date: CFRunLoopStop exception
  • Next by Date: Re: CFRunLoopStop exception
  • Previous by thread: Re: [NSMutableArray array]
  • Next by thread: How to force a 32-bit/64-bit universal app to start in 32-bit mode on Leopard?
  • Index(es):
    • Date
    • Thread