10-11-2009, 11:35 AM #1Why do you need to "alloc" for initWithCString and but doesn't for stringWithFormat ?
Why is it that these two are correct:
NSMutableString* CombinedText = [NSMutableString stringWithFormat:@"%d %d %d",1,2,3];
NSMutableString* CombinedText = [[NSMutableString alloc] initWithCString:"String Data" ];
Yet when I try to:
NSMutableString* CombinedText = [ [NSMutableString alloc] stringWithFormat:@"%d %d %d",1,2,3];
Likewise if I were to remove "alloc" from initWithCString.
10-11-2009, 03:38 PM #2
- Member Since
- Dec 13, 2007
- United States of America
- 2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09
Any time you initialize an object with [[ClassName alloc] init], you are explicitly allocating memory for it. When you use convenience methods, such as NSString's stringWithString: and string methods, the object is already allocated and initialized in the method and the result is returned.
The difference is that, by convention, you are responsible for releasing objects that you explicitly allocate with alloc and init. So, for every alloc there should be a release somewhere later in the code. Otherwise, you are leaking the memory used to store the object. When you use convenience methods that do not contain the words copy or new, you are typically not responsible for releasing them; they have been autoreleased before being returned for your use.
This is one of the most prevalent concepts that you'll need to understand in Objective-C and Cocoa programming. I highly recommend reading Apple's Memory Management Programming Guide for Cocoa.
10-11-2009, 03:50 PM #3
- Member Since
- Jun 25, 2005
- On the road
- 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
In the first one, you are using an NSString template that will allocate the space needed to return an NSString type for what you are creating.
In the second one, you are converting a C string which has different memory requirements, so you have to first allocated memory for an NSString type.
Note also that initWithCString is deprecated. See the NSString Class reference.
Lastly, the crasher occurs because you are trying to use a class method with an object instance. When you see the documentation mentioned above, you'll notice class methods have a plus (+) sign in front of them while an instance method has a minus (-) sign in front of them. A class method can only be used with the class name while a instance method works with instance variables you have have allocated.CameraTime - Time lapse photography for novice and advanced users.
When asking questions, post the version of your software. You'll receive better answers.
Please post your results to the thread as it is good feedback.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
By lexwissner in forum OS X - Operating SystemReplies: 17Last Post: 12-11-2012, 05:35 PM
By OneMoreThing... in forum Apple Rumors and ReportsReplies: 3Last Post: 04-12-2011, 10:04 AM
By Doug b in forum OS X - Apps and GamesReplies: 5Last Post: 02-13-2011, 04:42 PM
By oxband in forum OS X - Apps and GamesReplies: 1Last Post: 11-04-2009, 10:41 AM
By dannieboiz in forum Switcher HangoutReplies: 2Last Post: 10-15-2007, 09:50 AM