clear content from NSArrayController

Discussion in 'Mac Programming' started by MrFusion, Dec 8, 2007.

  1. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #1
    I want to clear the content from an NSArrayController programmatically, which seems an embarrassingly simple thing to do, yet which eludes me.

    The following line crashes my program:
    Code:
    [data removeObjects:[data arrangedObjects]];
    and
    Code:
    [data removeObjects:(NSArray *)[data arrangedObjects]];
    does the same thing.

    With NSMutableArray, you get at least removeAllObjects.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    If data is an NSArrayController then

    Code:
    [data content];
    
    should return the object (an NSArray or NSMutableArray presumably) holding the content so

    Code:
    
    [[data content] removeAllObjects];
    
    
    should work.

    I believe the reason what your doing doesn't work is that removing an item from the array causes the arrangedObjects array to change and indexing on the two is not the same...
     
  3. MrFusion thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #3
    I am using the NSArrayController itself to hold the content. My class only has an IBOutlet to a NSArrayController instance.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Is that working? My understanding is that NSArrayController (and the rest of the NSController classes) implement the controller layer of the MVC paradigm. They do not provide data storage: that is the job of the model layer. I've never seen an NSArrayController without a content array, either set via the content attribute or via bindings (which is the same thing in the end).
     
  5. MrFusion thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #5
    It does. :)
    But apparently not perfect. I am changing the code to include a model array.
     
  6. Nutter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #6
    Yes, managing your own model array is a good idea.

    Make sure that you use -mutableArrayValueForKey: to remove objects from the array.
     
  7. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #7
    I just thought about this a bit more since I just implemented a quick NSArrayController using a model array (instead of using the array controller directly). If you do it that way, you can just set your model array to nil or an empty array using KVC and it should work fine.
     

Share This Page