views:

33

answers:

1

I am wondering what differences such as disadvantages and/or advantages there are to declaring an NSString this way:

NSString *noInit = [NSString stringWithFormat:@"lolcatz %d", i];

as opposed to:

NSString *withInit = [[NSString alloc] initWithFormat:@"Hai %d", i];

What was the motivation of putting stringWithFormat instead of just having the initWithFormat way of initializing the string?

+4  A: 

stringWithFormat: returns an autoreleased string; initWithFormat: returns a string that must be released by the caller. The former is a so-called "convenience" method that is useful for short-lived strings, so the caller doesn't have to remember to call release.

mipadi
Ok, thanks that makes sense.
thyrgle
Beaten to the punch by 32 seconds. :-)
Sedate Alien