RE: personal convience constructors
RE: personal convience constructors
- Subject: RE: personal convience constructors
- From: "Josh Ferguson" <email@hidden>
- Date: Mon, 20 Jan 2003 16:34:08 -0600
- Thread-topic: personal convience constructors
Dave,
It would be...
+ (id)myObjWithStuff:(stuff *)stuff
{
id newObj = [[[self class] alloc] init];
...(insert initialization stuff here)...
return [newObj autoRelease];
}
The [self class] is optional...If your object is subclassed, it allows the method to return an instance of the same type as the subclass.
They do have to be class methods (you're requesting an instance of the class, and it's the Class's job to create instances, so it's only appropriate for it to be a class method). For more information on this, I'd strongly recommend reading through "The Objective-C Programming Language". Having a strong understand of how classes and objects actually work in Objective-C really helps you write better code (and make some cool optimizations to your code).
Josh Ferguson
MindVision Software
-----Original Message-----
From: David Cairns [
mailto:email@hidden]
Sent: Monday, January 20, 2003 4:17 PM
To: Cocoa Development List
Subject: personal convience constructors
thanks to everyone that helped me with my last problem.
here's another question for you now: How does one create one's own
convenience constructor? is it something as simple as
- (Obj *)myObj {
MyClass *newObj;
return [newObj autorelease];
}
???? or is it more difficult? do they have to be class methods?
-- dave
--------------------------------------------
"see you later, space cowboy..."
--------------------------------------------
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.