views:

2011

answers:

3

This is a very newbie questions, but does the iPhone API provide any data structures to work with (i.e. linked list, hash map, etc.)?

+14  A: 

You can use any data structure implemented on C or C++ with iPhone SDK.

For example, I use STL a lot.

Besides that, you can also use Cocoa's complex data structures available like:

  1. NSArray
  2. NSDictionary (hash)
  3. NSSet (set)

You can have a companion document on them here.

Pablo Santa Cruz
Technicality: Objective-C itself does not provide these classes—they are a part of *Cocoa*. Objective-C is just the language that is used. Not a huge deal, but it's a good idea to get into the habit of referring to these things correctly. ;)
htw
Agree. Going to change my answer to reflect what you are saying. Thanks!
Pablo Santa Cruz
A: 

The Objective-C language does not provide any inherent data structures like Linked Lists, etc. However, as it is based off of the C/C++ codebase, anything that can be implemented in C or C++ can be done directly in Objective-C, including Linked Lists and other data structures.

Jason
Objective-C is in no way "based off of the C++ codebase." C, yes, but anything C++ you have to go out of your way to use (and be somewhat masochistic)
Mike Abdullah
+3  A: 

Cocoa (a framework available both on Mac and iPhone) implements several common collection types, including NSArray, NSDictionary, and NSSet, as well as their mutable variants. (Leopard also introduced NSHashMap, NSHashTable, and NSPointerArray, which use weak references. However, these classes aren't currently available on iPhone, and would make little sense at the moment, since iPhone OS doesn't support garbage collection.) These classes are extremely fast, and are suitable for general-purpose use in any Cocoa application.

Beyond these provided structures, you have several choices: (1) create more complex structures using these as building blocks, (2) leverage existing third-party code, or (3) build your own data structures from scratch.

One option is CHDataStructures.framework, an open-source Objective-C framework which I maintain. It implements several other common data structures, such as stack/queue/deque, linked lists, sorted sets, and more. These structures adopt NSCoding and NSCopying (plus NSFastEnumeration on 10.5+), so they work seamlessly with native Objective-C code. The project allows you to build a static library for use on iPhone as well. Since this framework is open source, you can even include only the relevant code directly in your project if needed.

While you can use C++ and STL structures, I have found that mixing Objective-C and C++ tends to be more confusing and lead to in vexing bugs, especially for novices. This isn't a bash against C++, just a "when in Rome" principle. When using C++, STL is of course the preferred approach.

Quinn Taylor
Sure would be nice if those who feel the need to downvote would leave some feedback as to *why* they feel a response is "not helpful"...
Quinn Taylor
I do not believe that NSHashMap, NSHashTable or NSPointerArray are available on the iphone.
Carl Coryell-Martin
Carl is right, and since I didn't find this info anywhere else on SO, I want to state it clearly: NSHashMap, NSHashTable and NSPointerArray are not available in the iPhone SDK. See this question for a description of how to use NSValue to box pointers:http://stackoverflow.com/questions/393662/does-using-lists-of-structs-make-sense-in-cocoa
Felixyz
Ugh... that was the wrong link but... The point is: use NSValue to wrap pointers and put them in Cocoa collections.
Felixyz