Re: Noob question regarding Cocoa objects.
Re: Noob question regarding Cocoa objects.
- Subject: Re: Noob question regarding Cocoa objects.
- From: Scott Stevenson <email@hidden>
- Date: Mon, 27 Nov 2006 21:03:52 -0800
It looks like others have answered your questions, but there are a
couple of subtle points to add.
On Nov 27, 2006, at 7:07 PM, dan wrote:
2-By calling init (To initialize an object) it reaches the top most
object (NSObject) while executing all the init in the object tree?
Let's say there's objects A through Z, and Z inherits from NSObject.
So [a init] will call init inside B through NSObject?
I think you're mixing up "class" and "object."
When you create an object, the class will often often superclass's
implementation too. In a basic case, this means you call NSObject's -
init from your own init method. Doing this allows NSObject to handle
all the low-level details for you.
You still end up with on object -- one instance -- at the end.
3-Guessing my own objects (That I created) does not have to have an
"init" - but it could be labeled as "initWithParam", etc. But I
would have to create a logic that calls the super class init?
You should usually make an init method no matter what, if only to
setup the instance variables to reasonable defaults. This is much
safer and much cleaner.
4-Working with objects I think of alloc and init. But some objects
offer methods that does it for you? (Basically my last question
with an example)
Using NSString...
NSString *string1 = [NSString stringWithString:@"this is a test"];
Here stringWithString: actually fires off NSString init?
The short answer is yes. Conceptually, the implementation would look
something like this:
+ (NSString *) stringWithString: (NSString *)input
{
return [[[NSString alloc] initWithString:input] autorelease];
}
That's how you can think of it. In reality, it looks much different
because NSString is implemented in CoreFoundation.... yadda yadda
yadda. That doesn't matter to you at this point.
The most important thing to read up on right now is Cocoa memory
management. Specifically, referencing counting and alloc/release
versus autorelease.
The good news is all of this will start to get a lot easier with
Leopard.
- Scott
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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