|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|
|How much space should I allocate to Boot Camp?||Drewbonics||Windows, Linux & Others on the Mac||2||Mar 15, 2014 12:47 AM|
|Allocate Storage and clean it...||dirtnnasty||OS X Mavericks (10.9)||9||Dec 29, 2013 09:46 AM|
|iTunes cannot allocate resources||Danomac||Mac Applications and Mac App Store||0||Oct 27, 2013 09:49 AM|
|How much memory should I allocate for Windows 7?||anewfoundsin||Windows, Linux & Others on the Mac||2||Mar 16, 2013 04:21 PM|
|How much free space to allocate in external HDD?||dcslacker||Digital Video||2||Aug 5, 2012 05:21 AM|
All times are GMT -5. The time now is 07:48 AM.