Resolved Quick release of objects

Discussion in 'iOS Programming' started by carlosbutler, Feb 12, 2011.

  1. carlosbutler, Feb 12, 2011
    Last edited: Feb 13, 2011

    carlosbutler macrumors 6502a

    carlosbutler

    Joined:
    Feb 24, 2008
    Location:
    London City
    #1
    I have been looking around on the apple developer and this site (i guess not long enough since i can not find the answer) for my question.

    When releasing an object:
    Code:
    UILabel *label = [[UILabel alloc] init];
    [label release];
    Is it possible to release multiple objects in one go, by doing:
    Code:
    [obj1, obj2, obj3 release]
    I have tried this, and there is no compile, so obviously syntactically it is fine. But I am not sure if it is doing anything. Any suggestions?
     
  2. admanimal, Feb 12, 2011
    Last edited: Feb 12, 2011

    admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #2
    It is not possible to send a message (e.g. release) to multiple objects in this way. I'm pretty sure that all your code does is send release to obj3 because the comma (i.e. expression separator) operator evaluates to whatever is on its right side. It is a legal expression, but does nothing like what you think it does.
     
  3. carlosbutler thread starter macrumors 6502a

    carlosbutler

    Joined:
    Feb 24, 2008
    Location:
    London City
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    You could do something like this:

    Code:
    [[NSArray arrayWithObjects:obj1, obj2, obj3,nil] makeObjectsPerformSelector:@selector(release)];
    
    Note that this is much lower performances than calling release 3 times: you have to create a new NSArray object and then internally NSArray will iterate over it's contents.
     

Share This Page