3 ms·
the iOS framework lacks a built-in queue The convention for this sort of thing is to use an NSArray, inserting at position 0 to enqueue and removing the last o
by jrmg 13y ago
the iOS framework lacks a built-in queue
The convention for this sort of thing is to use an NSArray, inserting at position 0 to enqueue and removing the last object to dequeue. The NSArray class is actually written in a way that makes this efficient (for some great analysis see http://ridiculousfish.com/blog/posts/array.html http://ridiculousfish.com/blog/posts/array.html).
or a sorted list (as far as I know)
For sorted lists, I think you're right, you'd have to do it yourself.
The question a Cocoa old-hand might ask would be if you _really_ need it to be always-sorted, or if you can keep your objects in an array, dictionary or set and sort them before using or iterating - or maybe just iterate the collection in a specific order instead of sorting it at all.
If you do need to keep it sorted all the time, note that _inserting_ into an NSArray is also more efficient than you might assume. Keeping a sorted NSArray up to date yourself might not be expensive (it would be a bit more complicated than an auto-sorting class though, of course).
- jawngee 13y agoYou have to sort it yourself, but it's super easy. For NSArray's: NSArray * sortedArray=[array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { // your code here }]; For NSMutableArray's: [marray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { // your code here }];