What's the difference between a dictionary and an array?
Asked Answered
V

6

17

What is the difference between a dictionary and an array, especially when working with PLIST files? What are the advantages of using one over the other? Thanks!

Vitalize answered 23/2, 2012 at 15:30 Comment(0)
S
36

Both NSDictionary and NSArray are collection classes, i.e. the group together other objects.

An NSArray is an 'ordered collection' - every item in the collection has an integer index, so there is an explicit order to the items. If you swap the order of items in the collection then the collection is no longer the 'same' as the order is different. An object may appear more than once in the collection.

An NSSet is an 'unordered collection' - every item appears in a bag, the order doesn't matter and an object can only exist once in the bag.

An NSDictionary is an 'indexed collection' - every item in the collection has a key and can be retrieved with that key. An object may appear more than once, in that different keys may point to the same object, but a key can only appear once. A dictionary is also a form of 'hash table' if you have a computer science background.

When parsing PLISTs, Arrays and Dictionaries are the main types you deal with. When you edit a PLIST in Xcode - if you set something as an Array type, then all of it's children are listed as "Item 0, Item 1, Item 2..." whereas if you set it as a Dictionary type, then it's children are key:value pairs.

One significant use case for the difference types is as follows.

Imagine a magazine application which contains a number of articles. The order of the articles is important, and so you would store each article in an array. If you wanted to change the order of the articles, you would change the order of the array in the plist.

The articles themselves may be represented by Dictionaries, perhaps containing keys such as "TextFile", "Background", "ArticleType". You use a Dictionary because you may add more information to the dictionary at some point in the future, and the key:value mechanism makes your code understandable.

Spitz answered 23/2, 2012 at 15:42 Comment(1)
Thanks a lot for explaining this! It really helped.Vitalize
B
4

An array is just a sorted list of objects. A dictionary stores key-value pairs.

For example:

Array: obj1, obj2, ob3, ...

Dictionary:

{

    @"Name": @"Bob"

    @"Age": 20 (but NSDictionary can only store objects, so that would be a NSNumber)

}

There are no advantages or disadvantages, they are just two data structures, and you use the one you need.

Blackman answered 23/2, 2012 at 15:38 Comment(0)
H
4

The key difference is how you can access within them.

Both arrays and dictionaries are containers and can be read sequentally (e.g. arrays can be enumerated by means of an index and dictionaries by means of a key). But while arrays maintain the order amongs objects, dictionaries not.

In addition, with dictionaries you have the possibility to access a specific object with a specific key in a more user-friendly way (a mnemonic one). For example in a dictionary you know for sure that with a specific key, say "text", is associated a specific object, say NSString. The same could be valid for an array but with some difficulties. For example, how are you sure that at index 0 there is the specific object NSString?

About your question and as far I know (since Xcode 4), plists have as the root object a dictionary. For further info see how-do-you-change-a-plists-root-object-type-to-nsarray-in-xcode-4.

Hope it helps.

Hallowell answered 23/2, 2012 at 15:57 Comment(0)
R
2

Dictionaries associate a key with a value(object) and don't preserve the order of items. Array access is done by index - the order is preserved. PLIst add the ability use use XML for defining the data (key,value pairs).

Ru answered 23/2, 2012 at 15:40 Comment(0)
C
-1

NSDictionary is useful whwn u want to access a particular key value. NsArray is useful when u want to access a sequntial data.

Chet answered 18/1, 2013 at 6:33 Comment(0)
N
-2

NSDictionary does not retain its 'value' objects, but an NSMutableArray retain the objects added to it. For more information NSDictionary and NSArray

Niu answered 23/2, 2012 at 15:53 Comment(1)
This is incorrect - NSDictionary retains its values, but not its keys.Spitz

© 2022 - 2024 — McMap. All rights reserved.