• 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: Is a good practice to alloc an object and defer the initialization to another object?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is a good practice to alloc an object and defer the initialization to another object?


  • Subject: Re: Is a good practice to alloc an object and defer the initialization to another object?
  • From: Jesper Storm Bache <email@hidden>
  • Date: Wed, 10 Feb 2010 13:05:17 -0800
  • Acceptlanguage: en-US
  • Thread-topic: Is a good practice to alloc an object and defer the initialization to another object?

For this use case you can also use the fact that classes are objects.
Something like the following:
	Class theClass = nil;
	if (.....)
		theClass = [Class1 class];
	else
		theClass = [Class2 class];
	id obj = [[theClass alloc] initWithFoo:.... bar:....];

Jesper Storm Bache


On Feb 10, 2010, at 12:07 PM, Jens Alfke wrote:

>
> On Feb 10, 2010, at 5:27 AM, Giannandrea Castaldi wrote:
>
>> I would like to alloc an object and defer the initialization to
>> another object, is a good practice? The reason is that I can alloc
>> several types of objects but the init method have the same signature.
>
> It's unusual, but I've done it once or twice. It looks like:
>
> 	id obj;
> 	if (.....)
> 		obj = [Class1 alloc];
> 	else
> 		obj = [Class2 alloc];
> 	obj = [obj initWithFoo:.... bar:....];
>
> It's important that you reassign 'obj' to the value of the init call, since initializers are allowed to return a different object than the receiver, and the class-cluster design pattern takes advantage of this.
>
> —Jens
>
> _______________________________________________
>
> 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: 
 >Is a good practice to alloc an object and defer the initialization to another object? (From: Giannandrea Castaldi <email@hidden>)
 >Re: Is a good practice to alloc an object and defer the initialization to another object? (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: Using NSKeyedArchiver to save and restore state on iPhone apps
  • Next by Date: Re: Using NSKeyedArchiver to save and restore state on iPhone apps
  • Previous by thread: Re: Is a good practice to alloc an object and defer the initialization to another object?
  • Next by thread: Re: Is a good practice to alloc an object and defer the initialization to another object?
  • Index(es):
    • Date
    • Thread