Re: Zones, [[alloc] init], and all the new + methods
Re: Zones, [[alloc] init], and all the new + methods
- Subject: Re: Zones, [[alloc] init], and all the new + methods
- From: Thomas Lachand-Robert <email@hidden>
- Date: Wed, 26 Dec 2001 18:59:11 +0100
Le mercredi 26 dicembre 2001, ` 06:06 , Simson Garfinkel a icrit :
I've noticed that Apple is now shying away from [[<CLASS> alloc] init]
and has instead created a whole bunch of [<CLASS> <class>] constructors.
For example, instead of using
id dict = [[NSMutableDictionary alloc] init];
to create a dictionary, the correct approach is now:
id dict = [NSMutableDictionary dictionary];
No. They are different in effect, and have different usage. (And I don't
believe Apple promotes one or the other.) In fact,
dict = [NSMutableDictionary dictionary]
is equivalent to
dict = [[[NSMutableDictionary alloc] init] autorelease];
These convenience functions are intended to create "temporary" objects
(though you may want to retain them later).
Sometimes you need a temporary object. For instance, if you wish to return
more than one object/information from a function, you can return a
temporary NSArray or NSDictionary, like that:
NSDictionary* tellNameAndAge(id person) {
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:[person age]], @"age",
[NSString stringWithFormat:@"%@ %@", [person firstName],
[person lastName]], @"name",
nil];// ends the list
}
(It is actually better to use constant strings with identifiers for the
keys, but it is just an example.) Note that contained objects are also
temporary here. You can use this function like that:
NSDictionary* dict = tellNameAndAge(thatPerson);
int age = [[dict objectForKey: @"age"] intValue]; // I don't intend to
keep the NSNumber
[myInstanceVariable release]; // throws previous value
myInstanceVariable = [[dict objectForKey: @"name"] retain]; // I need to
keep this information
NSLog(@"Name is %@ and age is %i.", myInstanceVariable, age);
Later on you will need to release 'myInstanceVariable' because you
retained it, but there is nothing more to do for the dictionary or the
NSNumber: they will be destroyed automatically.
This sort of use is very common with general classes like arrays,
dictionaries, strings, etc., that's why Apple provides them as a
convenience. Note that if you intend to keep an object for future use, it
is more efficient to use [[xxx alloc] init] than the convenience method +
retain. But the example shows why it is useful. This sort of simple things
is very difficult to do in C++ for instance: the user of the function has
to remember to explicitely free the given objects. If he doesn't, then
there is a memory leak. If the returned objects are not intended to be
freed, there is a crash. (Both are very frequent with C++ apps.)
This feature is a key point in the retain/release mechanism, and a great
advantage of Cocoa.
Thomas Lachand-Robert
********************** email@hidden
<< Et le chemin est long du projet ` la chose. >> Molihre, Tartuffe.