Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDictionary Vs. NSArray

I am reading on objective-c (a nerd ranch book), and I can't help thinking about this question: How do I decide which collection type, NSArray or NSDictionary (both with or w/o their mutable subclasses), to use when reading content from URL?

Let's say am reading JSON data from a PHP script (a scenario am dealing with), which to use? I know it is stated in many references that it depends on structure of data (i.e. JSON), but could a clear outline of the two structures be outlined?

Thank you all for helping :)

like image 205
sys_debug Avatar asked Feb 01 '13 09:02

sys_debug


People also ask

Which is faster NSArray or NSMutableArray?

NSMutableArray and NSArray both are build on CFArray , performance/complexity should be same. The access time for a value in the array is guaranteed to be at worst O(lg N) for any implementation, current and future, but will often be O(1) (constant time).

What's a difference between NSArray and NSSet?

An NSSet is much like an NSArray, the only difference is that the objects it holds are not ordered. So when you retrieve them they may come back in any random order, based on how easy it is for the system to retrieve them.

What is the difference between NSDictionary and NSMutableDictionary?

Main Difference is:NSMutableDictionary is derived from NSDictionary, it has all the methods of NSDictionary. NSMutableDictionary is mutable( can be modified) but NSDictionary is immutable (can not be modified).

What is the difference between NSMapTable vs NSDictionary?

NSDictionary / NSMutableDictionary copies keys, and holds strong references to values. NSMapTable is mutable, without an immutable counterpart. NSMapTable can hold keys and values with weak references, in such a way that entries are removed when either the key or value is deallocated.


2 Answers

NSArray is basically just an ordered collection of objects, which can be accessed by index.
NSDictionary provides access to its objects by key(typically NSStrings, but could be any object type like hash table).

To generate an object graph from a JSON string loaded via a URL, you use NSJSONSerialization, which generates an Objective-C object structure. The resulting object depends on the JSON string. If the top-level element in your JSON is an array (starts with "["), you'll get an NSArray. If the top-level element is a JSON object (starts with "{"), you'll get an NSDictionary.

like image 165
Florian Avatar answered Sep 21 '22 20:09

Florian


You want to use NSArray when ever you have a collection of the same type of objects, and NSDictionary when you have attributes on an object.

If you have, lets say a person object containing a name, a phone number and an email you would put it in a dictionary.

Doing so allows the order of the values to be random, and gives you a more reliable code.

If you want to have more then one person you can then put the person objects in an array.

Doing so allow you to iterate the user objects.

like image 40
EsbenB Avatar answered Sep 22 '22 20:09

EsbenB