user1048633
user1048633

Reputation: 13

When Do classes need to be allocated?

In my program I have

NSString *stringOne = [NSString stringWithFormat:@"Hello World"];
[variable insertText:stringOne];

and the code runs fine. I know that the 'stringWithFormat:' method initiates the object, but where does the alloc happen? And why is it not needed here?

I can do the same with NSSound

NSSound *favoriteSong = [NSSound soundNamed:@"Friday"];
[favoriteSong play];

this will run, too. I know that 'soundNamed:' returns & initiates the object but the NSSound was never allocated.

I always assumed that I would have to do the following..

NSSound *favoriteSong = [[NSSound alloc]initWithBlablanla];

and then continue from there in order for everything to work.

What Im asking is, where does the allocate happen?

Upvotes: 1

Views: 81

Answers (5)

zaph
zaph

Reputation: 112857

stringWithFormat is a connivence class method declared as:

+ (id)stringWithFormat:(NSString *)format, ...

Notice the "+" in font, that designates a class method. The documentations states:

Returns a string created by using a given format string as a template into which the remaining argument values are substituted.

But even this is not necessary, one can just as correctly write:

[variable insertText:@"Hello World"];

It would probably be worthwhile to read Apple's documentation on Objective-C.

Upvotes: 1

Dmytro
Dmytro

Reputation: 2552

Basically stringWithFormat allocates the object, puts it to an autorelese pool and returns it to you. You can find GNUStep implementation of NSString here

Upvotes: 0

Sean
Sean

Reputation: 5820

Those sort of methods are called convenience (class) methods which return an autoreleased object. In other words, somewhere in the implementation for stringWithFormat is something similar to

NSString * string = [[[NSString alloc] init] autorelease];

Upvotes: 0

Oliver
Oliver

Reputation: 23510

Some methods like soundNamed allocate an autoreleased object and return it. As an example, and let's imagine those 2 methods exist :

calling :

NSSound *favoriteSong = [NSSound soundNamed:@"Friday"];

return a NSSound allocated object, inited with @"Friday". This returned object is autoreleased.

calling :

NSSound *favoriteSong = [[NSSound alloc] initWithSoundNamed:@"Friday"];

return a NSSound allocated object, inited with @"Friday". This returned object is retained, and must be deallocated.

Those two calls would do the same thing :

NSSound *favoriteSong = [NSSound soundNamed:@"Friday"];
NSSound *favoriteSong = [[[NSSound alloc] initWithSoundNamed:@"Friday"] autorelease];

As a shortcut, soundNamed is something like :

+ (id) soundNamed:(NSString*)name
{
    NSSound* aSound = [[NSSound alloc] initWithSoundNamed:name];
    if (!aSound) return nil;

    return [aSound autorelease];
}

Upvotes: 1

sidyll
sidyll

Reputation: 59297

When Do classes need to be allocated?

You shouldn't bother with class objects too much, it's all done automatically. I really think we're not talking about the same thing though. Maybe you mean "objects returned by class methods" instead of "classes". Something like:

When do objects returned by class methods need to be allocated?

Looks more appropriate.

where does the alloc happen?

You answered that, right before these words. It happens inside the +stringWithFormat: method.

And why is it not needed here?

It's not needed in your side, as it was already done in the library side.

I know that 'soundNamed:' returns & initiates the object but the NSSound was never allocated.

Of course it was. If it exists, it was allocated*. Again, it was allocated inside the +soundNamed: method.


* At least in normal means. Maybe someone can cite a corner case.

Upvotes: 0

Related Questions