How to display an NSSet in a UITableView?
Asked Answered
B

1

10

How would you proceed to display the content of an NSSet in a UITableView?

As you know, the table view will ask for the element at a given row, but since the NSSet elements aren't ordered, this doesn't mix well.

My current solution is to iterate through the NSSet until I reach the element at a given index, but this really doesn't feel right.

You may ask why I don't use an NSArray instead. This is because I'm using Core Data, which uses NSSets to store collection of elements.

Any help would be appreciated.

Backwoodsman answered 17/9, 2009 at 14:49 Comment(0)
B
29

Since you're only displaying the elements, why not take the NSSet, and call -[allObjects] on it to get a NSArray?

If your data changes, you'll have to re-fetch anyway, even with a NSSet, and apart from the initial conversion from the NSSet, using the array is quite fast.

Bladder answered 17/9, 2009 at 15:25 Comment(4)
So could we safely assume that 2 consecutive calls to "allObjects" returns the elements in the same order (when the NSSet has not been changed) ? Because the documentation only says that the "order isn't defined", it does not mention that the order is stable. And if it is stable now, are you sure it will stay stable in future iOS versions ? I am not so sure about relying on this function to get elements that I can display in a table view.Disject
@ChristopheFondacci just call allObjects once and store the result in a variable until the VC is dismissed, or until the set is changed again.Weise
@VictorJalencas yes I know how to do that, that is not my question. My question is about the implementation of allObjects.Disject
@ChristopheFondacci it's probably not a good idea to call allObjects twice expecting to get the same result. Even if for some sets it works, being a class cluster the result may depend on the count of objects. Hence my advice to call it only once and keep the result instead of calling allObjects each time if you need repeatability. Even if you don't need it, iterating over the same array will be faster than getting a new one.Weise

© 2022 - 2024 — McMap. All rights reserved.