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

前端 未结 6 774
长发绾君心
长发绾君心 2020-12-15 07:04

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!

6条回答
  •  抹茶落季
    2020-12-15 07:30

    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.

提交回复
热议问题