How Delete the oldest object in NSMutableSet?

Discussion in 'iOS Programming' started by Sergio10, Nov 11, 2009.

  1. macrumors regular

    Joined:
    Oct 3, 2007
    #1
    Hi,

    I have sorted container NSMutableSet with NSString objects. And need the oldest object which was inserted. How to to do this?

    PHP:
    [mySet removeLastObject];  // doesn't remove the oldest string.

    Thank you.
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    First, removeLastObject: is an instance method for NSMutableArray, not NSMutableSet.

    Second, you'll probably need to keep track of the "oldest" object separately and then call
    Code:
    [mySet removeObject:oldestObject];
     
  3. macrumors regular

    xsmasher

    Joined:
    Jul 18, 2008
    #3
    NSSet and NSMutableSet are unordered collections, so you should probably choose some other data structure if you need to store items in order - like NSMutableArray.

    The method removeLastObject will give you the last object that was added at the end of the array. If you want the "oldest" item in an array, you probably want to do something like this:

    Code:
    id oldest = [myMutableArray objectAtIndex:0];
    [muMutableArray removeObjectAtIndex:0];
    
    Remember to retain "oldest" if you need it to stick around - it might get dealloc'd when the array releases it.
     

Share This Page