FROM : Steve Christensen
DATE : Thu Jul 03 22:53:34 2008
On Jul 3, 2008, at 11:57 AM, Chris Paveglio wrote:
> My code is like this:
>
> NSMutableString *theSettings;
> theSettings = [[NSMutableString alloc] init];
>
> //myPrefs is an array of strings, each item is like "Library/Safari"
>
> int i;
> for (i = 0; i < 8; i++
> {
> theSettings = [NSHomeDirectory() stringByAppendingPathComponent:
> [myPrefs objectAtIndex:i]];
> ....
> }
>
> Thinking about it, do I need the alloc and init commands? Sometimes
> I am unsure about what needs alloc or init versus what I can just
> declare as a variable without doing that.
I noticed a three things right off:
1. -stringByAppendingPathComponent: returns a NSString, yet you're
saving the value to a variable of type NSMutableString.
2. At the top of your code you're initializing theSettings when
you're just going to overwrite the value in your loop (and leak
memory in the process).
3. If you were actually trying to initialize theSettings for a good
reason, you should be using NSMutableString's -initWithCapacity:
method vs the more generic -init method.
DATE : Thu Jul 03 22:53:34 2008
On Jul 3, 2008, at 11:57 AM, Chris Paveglio wrote:
> My code is like this:
>
> NSMutableString *theSettings;
> theSettings = [[NSMutableString alloc] init];
>
> //myPrefs is an array of strings, each item is like "Library/Safari"
>
> int i;
> for (i = 0; i < 8; i++
> {
> theSettings = [NSHomeDirectory() stringByAppendingPathComponent:
> [myPrefs objectAtIndex:i]];
> ....
> }
>
> Thinking about it, do I need the alloc and init commands? Sometimes
> I am unsure about what needs alloc or init versus what I can just
> declare as a variable without doing that.
I noticed a three things right off:
1. -stringByAppendingPathComponent: returns a NSString, yet you're
saving the value to a variable of type NSMutableString.
2. At the top of your code you're initializing theSettings when
you're just going to overwrite the value in your loop (and leak
memory in the process).
3. If you were actually trying to initialize theSettings for a good
reason, you should be using NSMutableString's -initWithCapacity:
method vs the more generic -init method.






Cocoa mail archive

