Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of Java's List in Objective-C?

I wonder if Objective-C offers support for Lists? Or .. is NSMutableArray a way to go, instead?

like image 455
James Raitsev Avatar asked Nov 15 '11 21:11

James Raitsev


2 Answers

Yes NSMutableArray (Mac, iOS) would in most cases be the appropriate class for lists (with NSArray being the immutable counterpart).

Contrary to Java's collection classes Objective-C's (or rather Foundation's) arrays are opaque class clusters that completely hide their implementation.

Edit: snip (see comment by orange80)

While Java has a ton of collection classes such as:

  • HashSet
  • TreeSet
  • LinkedHashSet
  • ArrayList
  • LinkedList
  • PriorityQueue
  • HashMap
  • TreeMap
  • LinkedHashMap
  • WeakHashMap
  • IdentityHashMap
  • CopyOnWriteArrayList
  • CopyOnWriteArraySet
  • EnumSet
  • EnumMap
  • ConcurrentLinkedQueue
  • LinkedBlockingQueue
  • ArrayBlockingQueue
  • PriorityBlockingQueue
  • DelayQueue
  • SynchronousQueue
  • ConcurrentHashMap

Objective-C however (again, actually the Foundation SDK) only provides a very limited number of collection classes:

  • CFMutableDictionary
  • CFMutableBag
  • CFMutableBitVector
  • CFMutableSet
  • CFMutableArray
  • CFBinaryHeap
  • CFMutableTree

or preferably their NS-equivalents:

  • NSMutableDictionary
  • NSDictionary
  • NSMutableSet
  • NSSet
  • NSCountedSet
  • NSMutableArray
  • NSArray

For a full insight in the matter I recommend this read: http://ridiculousfish.com/blog/posts/array.html (the author is a member of Apple's Foundation Team)

like image 138
Regexident Avatar answered Oct 19 '22 05:10

Regexident


Generally speaking, you'll want to use NSMutableArray for equivalent behaviour (though the implementation is very different.) You also have the option of using CFMutableArray (a C-based API that is a little more flexible than NSMutableArray and is toll-free bridged with it.) If you're using C++ rather than (or in addition to) Objective-C, you have any of the STL collection types (such as std::list<T>, std::vector<T>, etc.) whose suitability will depend on your exact use case.

like image 29
Jonathan Grynspan Avatar answered Oct 19 '22 07:10

Jonathan Grynspan