Search code examples
swiftnsarraycontroller

How to empty a NSArrayController in swift


I suppose the answer is quite easy, but i did not manage to find an answer:

I am using a nscollectionview whos content is managed via an nsarraycontroller. adding objects works as intended but i do not find any method to remove all objects. there are several remove methods but something like .clear, .empty or .removeAll is not available.

Help appreciated :-)


Solution

  • You should be able to do what you want like so :

    let range : NSRange = NSMakeRange(0, yourArrayController.arrangedObjects.count)
    let indexSet : NSIndexSet = NSIndexSet(indexesInRange: range)
    
    yourArrayController.removeObjectsAtArrangedObjectIndexes(indexSet)