Re: Lack of Initializers or Factory Methods
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