|Dec 16, 2008, 03:22 PM||#1|
When to allocate?
Can someone please explain the diffrence of the following and when to use what..?
NSString *string = [[NSString alloc] initWithFormat:@"hello"];
NSString *string = @"hello";
|Dec 16, 2008, 05:20 PM||#2|
NSString *string = [[NSString alloc] initWithFormat:@"hello"]; //vs. NSString *string = @"hello";
The first one - the alloc/init - creates a new NSString object for you, which you manage (retain/release) as appropriate. Also, the init method you chose, initWithFormat, can take a format string a list of variables - so if you need to combine a list of variables into a string, you can use:
string = [[NSString alloc] initWithFormat:@"I lost my %@ in %@", bodyPart, city];
I'd use the @"Hello" wherever I have a string that will always be the same every time I run my program, and the longer form whenever I need to construct a string that will be different every time I run my program.
Available Now:Dead Panic, a strategic zombie shooter
New Blog Post:How to make your first game for less than $1,000
Last edited by xsmasher; Dec 16, 2008 at 05:22 PM. Reason: reduce size of quote
|Thread Tools||Search this Thread|
|thread||Thread Starter||Forum||Replies||Last Post|
|New to Apple: Should I start with a Refurb?||BeerNap||MacBook Pro||5||Jan 13, 2011 02:52 PM|
|What is the MINIMUM space I NEED to allocate in VMWare for Windows 7 to work?||vader1990||MacBook Pro||6||Jan 24, 2009 12:16 AM|
|When to release an alloc. textField?||yrvaken2||iPhone/iPad Programming||5||Jan 5, 2009 01:40 PM|
|Obj-C: When Do I need to Allocate?||Soulstorm||Mac Programming||3||Apr 26, 2006 01:42 PM|
|How much space to allocate for XP||lamina||Windows, Linux & Others on the Mac||7||Apr 7, 2006 08:56 AM|
All times are GMT -5. The time now is 03:59 PM.