• 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: Lack of Initializers or Factory Methods
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Lack of Initializers or Factory Methods


  • Subject: Re: Lack of Initializers or Factory Methods
  • From: j o a r <email@hidden>
  • Date: Mon, 3 Nov 2008 08:57:53 -0800


On Nov 3, 2008, at 8:37 AM, Michael Ash wrote:

If the method is declared like this then you should assume that you
get a subclass:

+ (id)foo;

And if it is declared like this then you should not assume that:

+ (Foo *)foo;

The difference being the return type. The 'id' return type implicitly
means "this will return whatever is appropriate for the receiver".
(This should probably be documented better.) The 'Foo *' return type
tells you only to expect a Foo, and not rely on receiving any sort of
subclass.

The latter form is rare in Cocoa but shows up in e.g. NSParagraphStyle.


My understanding is that that this is the general pattern to use:

* Return (id) from initializers, and convenience factory methods.
* Return (Foo*) for shared instances.

There are some exceptions to these "rules" in Cocoa, but they are few and far in between.
Note that "+[NSParagraphStyle defaultParagraphStyle]" fits this pattern, since it returns a shared instance.


j o a r


_______________________________________________

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: Lack of Initializers or Factory Methods
      • From: "Michael Ash" <email@hidden>
References: 
 >Re: Lack of Initializers or Factory Methods (From: Gordon Apple <email@hidden>)
 >Re: Lack of Initializers or Factory Methods (From: "Michael Ash" <email@hidden>)

  • Prev by Date: Re: Lack of Initializers or Factory Methods
  • Next by Date: Use of cURL and OpenSSL versus CF and NS Classes
  • Previous by thread: Re: Lack of Initializers or Factory Methods
  • Next by thread: Re: Lack of Initializers or Factory Methods
  • Index(es):
    • Date
    • Thread