Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zrbecker

macrumors member
Original poster
Jun 28, 2009
58
0
Is using autorelease a lot of overhead for iPhone applications?

Is it enough overhead to worry about the following...

Code:
// This gives me a NSString that will autorelease. (I think)
NSString *string1 = [NSString stringWithFormat:@"%@, %@",
    lastName, firstName];

NSString *string2 = [[NSString alloc] initWithFormat:@"%@, %@",
    lastName, firstName];

// Do some stuff with strings

[string2 release];

Would string2 be better/worse implementation? Or does it not matter and I am worrying too much about it.

Thanks!

I am new to iPhone Programming and Objective-C.
 
I wouldn't worry about it too much. The main issue is probably if you create autoreleased objects in a loop. If the code you showed were in a loop executed 1000x then I would do my best to not use autoreleased strings.

In general, if it is simple to use an alloc/inited/released object then do so. This is considered the 'best practice.' If you are returning objects from methods then they should almost always be autoreleased. If you create a few autoreleased strings here and there it won't have a noticeable impact on your app.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.