Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread Display Modes
Old Dec 16, 2008, 02:22 PM   #1
macrumors newbie
Join Date: Sep 2008
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";
I am struggling a bit to get the hang of this memory efficient programing with releasing objects and stuff.
yrvaken2 is offline   0 Reply With Quote
Old Dec 16, 2008, 04:20 PM   #2
macrumors regular
Join Date: Jul 2008
 NSString *string = [[NSString alloc] initWithFormat:@"hello"];
NSString *string = @"hello";
They differ in terms of memory management and flexibility. The second method, @"hello" , is a literal string - you don't need to retain or release it. It will always be around. Handy for creating constants. Even if you use this line in a loop, you are NOT creating a new object every time. There's only one @"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];
You can't do that with the @ method - literal strings are defined when the program is compiled, not when it's running.

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.

Last edited by xsmasher; Dec 16, 2008 at 04:22 PM. Reason: reduce size of quote
xsmasher is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
New to Apple: Should I start with a Refurb? BeerNap MacBook Pro 5 Jan 13, 2011 01:52 PM
What is the MINIMUM space I NEED to allocate in VMWare for Windows 7 to work? vader1990 MacBook Pro 6 Jan 23, 2009 11:16 PM
When to release an alloc. textField? yrvaken2 iPhone/iPad Programming 5 Jan 5, 2009 12:40 PM
Obj-C: When Do I need to Allocate? Soulstorm Mac Programming 3 Apr 26, 2006 12:42 PM
How much space to allocate for XP lamina Windows, Linux & Others on the Mac 7 Apr 7, 2006 07:56 AM

All times are GMT -5. The time now is 09:29 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013,, LLC