In java, one can make use of the generic LinkedList
to improve the efficiency when objects are often added to the front of the list. What is its equivalent in the iOS frameworks?
You need to understand that in Foundation classes like NSArray
, etc, is not what you learned as an array, etc in your beginning programming class. In particular, it doesn't have the performance characteristics you would normally associate to an array.
On this point, there are many nice blog posts, e.g. one by Ridiculous Fish and another by Cocoa with Love
So, as everybody else said, just use NSMutableArray
.
NSMutableArray is closest to this. Despite the name, it's closer to a list than an array. However, "appending to and removing elements from either end take constant time", according to this.
Also, what about this, a third-party implementation: https://github.com/mschettler/NSLinkedList
There is no direct equivalent. Writing a linked list yourself is pretty easy, but I doubt that you will gain a lot of performance compared to NSMutableArray
.
There are a couple of different linked list implementations in the open source CHDataStructures.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With