Is there any difference from Memory perspective between the following two lines
NSString *dbFilePath =[[NSString alloc]initWithString:[[NSBundle mainBundle] pathForResource:dbName ofType:nil]];
and
NSString *dbFilePath =[[[NSBundle mainBundle] pathForResource:dbName ofType:nil] retain];
“[NSString alloc]initWithString:” or just “retain”
Thanks in advance
Practically, there is no difference. However, in the first case, you temporarily have one more
NSStringobject existing than in the second case, i.e. the object returned bypathForResourcewhich will be autoreleased shortly after, and it’s surviving copy.In the second case, no copy is made. Instead, the object returned by
pathForResourceis retained directly. One object less at peak time.I find the second piece of code more straightforward.
However, I wonder why you retain something that is referenced from the stack. I would expect
dbFilePathinstead to be something more persistent, like a member variable.