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?
Some methods like
soundNamedallocate an autoreleased object and return it. As an example, and let’s imagine those 2 methods exist :calling :
return a NSSound allocated object, inited with @”Friday”. This returned object is autoreleased.
calling :
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 :
As a shortcut, soundNamed is something like :