Sort Wrappers for NSSet

I need to populate a UITableView from an NSSet data source. Obviously, I want the table sorted by some criteria.

I could just create a sorted NSArray with [mySet sortedArrayUsing...] and use it for my table data source. But I need something more powerful: the underlying NSSet ( mySet ) changes over time, and I want the UITableView update accordingly (still sorted, of course).

My best bet so far is attaching observers to an NSSet and updating NSArray and UITableView with every change to NSSet . However, it contains quite a lot of code and is not very efficient.

So I would like to have something like this, I think:

 NSArray* sortedWrapperArray = [SortedWrapper wrapperFor:mySet sortUsing:sortCriteria]; 

where sortedWrapperArray automatically updated when the underlying set changes.

Is there something similar or similar in the Objective-C / Cocoa world?

+4
source share
2 answers

If your application will only run on iOS 5.0 or later, you can use NSOrderedSet

+1
source

If your base collection is NSSet , you will have to sort it from scratch every time the set changes, spending O(N*Log(N)) for that time. I think that a much more effective way to solve this problem is to replace NSSet with NSMutableArray and save it in the settings. Adding and removing items in a sorted array will still be O(N) : although you can find a position using binary search (see indexOfObject:inSortedRange:options:usingComparator: ), inserting / deleting is usually O(N) (at very low constant), Preserving the uniqueness property of the set is also easy: if in the binary search the same element that you are going to insert is found, just skip the insert. The same values ​​would check O(Log(N))

+3
source

Source: https://habr.com/ru/post/1389807/


All Articles