Number of item in a NSArrayCollection
All in the title
except : what is an NSArrayCollection ?
I suppose you mean : number of objects managed by NSArrayController ?
If so, you could:
let controller = NSArrayController()
// add objects
print((controller.arrangedObjects as! [AnyObject]).count)
Yes.
Thx.
So I switched my little app with NSTableViews to NSArray to NSArrayCollection.
It is working but to get the number of items I still need to use a NSArray for the moment... I'd like to remove it and only use only the NSArrayCollection.
So for the viewFor tableColumn: NSTableColumn? (for example), I need to know how many objects are added in the NSCollectionView. I create it programmatically for information, not in IB.
Thx.
Also, I mean NSArrayController and not NSArrayCollection, my mistake...
So, did you try:
let controller = NSArrayController()
// add objects
controller.addObject(125 as Any)
controller.addObject("Hello" as Any)
print((controller.arrangedObjects as! [AnyObject]).count). // as! [AnyObject] needed because not an array.
You get 2.
See interesting discussion here: https://stackoverflow.com/questions/30965208/how-to-iterate-over-an-nsarraycontroller-contents-in-swift
If OK, don't forget to close the thread by marking correct answer ; otherwise explain what is not working.
And please also close or respond on some other threads you opened.
Alright, it is working !
But now, how to get an item at index(n) ?
In the viewFor tableColumn: NSTableColumn?, the [row] method failed :
let theTracks = theArrayCollection.arrangedObjects as! [ITLibMediaItem]
let theTrack = theTracks[row] >>> fail ("NSArray element failed to match the Swift Array Element type")
Why ?
Thx.
Well, I think it is because I used the arrayController.add method instead of arrayController.addObject like in your example...
A dump like in your link in the iteration works better now.
Thx.
Great. Note that if you just add, the count is 0. Because it does not add to the array. That's a pretty subtile difference.
- func add(Any?)
Creates and adds a new object to the receiver’s content and arranged objects.
- func addObject(Any)
Adds object to the receiver’s content collection and the arranged objects array.
Thx for this tip.
And is it possible to add an entire array directly ?
Thx.