Results 1 to 3 of 3
  1. #1

    Bracer's Avatar
    Member Since
    Oct 07, 2009
    Why 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" ];
    The question is, why is it that you have to "alloc" when you are using the "initWithCString" function yet doesn't have to when you do stringWithFormat, I am puzzled because they BOTH returns a NSMutableString, so shouldn't they have similar initialization protocols ?
    Yet when I try to:
    NSMutableString* CombinedText = [ [NSMutableString alloc] stringWithFormat:@"%d %d %d",1,2,3];
    It crashes ?
    Likewise if I were to remove "alloc" from initWithCString.

  2. #2

    nabl's Avatar
    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.

  3. #3

    xstep's Avatar
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Please help! Deskjet 3050 doesn't show up in "add printer" section!
    By lexwissner in forum OS X - Operating System
    Replies: 17
    Last Post: 12-11-2012, 05:35 PM
  2. "Post PC" Doesn't Mean "Sans PC"
    By OneMoreThing... in forum Apple Rumors and Reports
    Replies: 3
    Last Post: 04-12-2011, 10:04 AM
  3. Safari's "mouse gestures" extension doesn't fully work?
    By Doug b in forum OS X - Apps and Games
    Replies: 5
    Last Post: 02-13-2011, 04:42 PM
  4. "Date added" doesn't show up in my iTunes. Any idea why?
    By oxband in forum OS X - Apps and Games
    Replies: 1
    Last Post: 11-04-2009, 10:41 AM
  5. "Tap" doesn't go to drop down menu and check boxes?
    By dannieboiz in forum Switcher Hangout
    Replies: 2
    Last Post: 10-15-2007, 09:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts