How to RELEASE a NSString that we set Directly to the Label?

Discussion in 'iOS Development' started by whocaresit, Apr 22, 2009.

  1. whocaresit

    whocaresit New Member

    Joined:
    Mar 11, 2009
    Messages:
    48
    Likes Received:
    0
    I know it's required that we should RELEASE everything we ALLOC.

    How would we release the NSString then?

    Code:
    	sliderLabel.text = [[NSString alloc] initWithFormat:@"%@",sliderVal];
    
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    sliderLabel.text = [NSString] stringWithFormat

    Please Register or Log in to view images

    "%@",sliderVal];

    That is autoreleased.

    Alternatively,

    sliderLabel.text = [[[NSString alloc] initWithFormat

    Please Register or Log in to view images

    "%@",sliderVal] autorelease];
  3. Just_For_Now

    Just_For_Now Active Member

    Joined:
    Mar 21, 2009
    Messages:
    1,898
    Likes Received:
    1
    Device:
    4G iPod touch
    I usually use this method:
    Code:
     Label.text = [NSString stringWithFormat:@"%i", value]; 
  4. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Just for now, that's a syntax error, but it was my fault. I clearly forgot to remove the right bracket after copy/pasting the original method.

    Any way,
    label.text = [NSString stringWithFormat

    Please Register or Log in to view images

    "Some Format: %@", someObject];
  5. whocaresit

    whocaresit New Member

    Joined:
    Mar 11, 2009
    Messages:
    48
    Likes Received:
    0
    Good. So we can depend on it most of the time right instead of assigning to a variable and releasing it ourselves?

    So, we need the autorelease keyword when using initWithFormat Right but not required for stringWithFormat?
  6. bddckr

    bddckr Active Member

    Joined:
    Dec 2, 2007
    Messages:
    1,434
    Likes Received:
    18
    Device:
    iPhone 4 (Black)
    You call am -init method to an allocated object, that said, if you allocate it, you are responsible to release it.
    Most class methods (that are the ones that use the plus sign instead of the minus one) handle the releasing on their own. So +stringWithFormat: will autorelease the NSString.

    To avoid asking these questions for every new class you'll be working with you should consider to use the documentation. I think the Objective C 2.0 Programming Guide should cover this stuff.
  7. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    I wouldn't assume that all class methods release on their own, but it's generally safe to accept that many class methods either release on their own or return an autoreleased object.

    The difference is that a class method is something that the class does. Ie, if you have a string, and you simply want the NSString class to do something with it and return something else. Get an int value from a string, for example.

    An instance method is a method that you'd use while working with an instance of the string.


    In your situation, what you are trying to do is simply feed some text in a certain format to the NSString object and get an NSString object back. Hence [NSString stringWithFormat

    Please Register or Log in to view images

    "Some Format:\t%@", someObject];
    In this case, the NSString generates a formatted string and autoreleases it to reduce its retain count.

    If you were intending to hang on to the string, you would need to use the instance method, and manage the memory for it yourself. Ie, alloc and dealloc (and don't hesitate to use malloc(), calloc(), and realloc() either)
    [[NSString alloc] initWithFormat

    Please Register or Log in to view images

    "Some Format:\t%@", someObject];
    [NSString release];

Share This Page