• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Does this caution need fixed? (newb)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Does this caution need fixed? (newb)


  • Subject: Re: Does this caution need fixed? (newb)
  • From: Steve Christensen <email@hidden>
  • Date: Thu, 03 Jul 2008 13:53:34 -0700

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-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: Does this caution need fixed? (newb) (From: Chris Paveglio <email@hidden>)

  • Prev by Date: Re: Contextual menu in Finder
  • Next by Date: Re: Does this caution need fixed? (newb)
  • Previous by thread: Re: Does this caution need fixed? (newb)
  • Next by thread: Re: Does this caution need fixed? (newb)
  • Index(es):
    • Date
    • Thread