0
votes

An example in swift tour in https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html#//apple_ref/doc/uid/TP40014097-CH2-ID1

var occupations = [
  "Malcolm": "Captain",
  "Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"

The final result of occupations is :

["Kaylee": "Mechanic", "Jayne": "Public Relations", "Malcolm": "Captain"]

My question:

  1. Is var occupations a Map?
  2. In what order does new item be added?
3
The type of occupations is called a "Dictionary" in Swift. But dictionaries are not ordered.Eric Aya
To elaborate on the second point: a dictionary is a key-value-store. It knows the value for the key "Kaylee" is "Mechanic", it cannot tell you which value is at position 7 e.g.. That is not what dictionaries are made for - they are (as Eric already wrote) unordered.luk2302

3 Answers

3
votes

The existing answers are highly likely to be confusing depending on what sort of programming background you come from.

var occupations = [
  "Malcolm": "Captain",
  "Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"

Given that code snippet, the type of occupations is a Swift Dictionary. As the other answers point out, a dictionary, in Swift, is a collection of key-value pairs.

Other languages have a similar data structure, but may refer to it by a different name.

The list could go on. But these all represent roughly the same data structure: a store of key & value pairs.

In Swift, dictionaries do not have an order. Any perceived ordering you notice from printing or iterating over items in a dictionary is just that--perceived. It should not be relied on.

If you need an ordered dictionary, you will need to implement it on your own. Although I'm sure someone has probably already implemented it and you can find it on github. A very simple implementation for an ordered pairs of data could simply involve an array of tuples, but you wouldn't be able to do the same key look up you can with dictionaries.

What is important here is that there is no defined order for data in a Swift dictionary, so any perceived ordering that is happening should not be relied on.

0
votes
  1. "occupations" is a dictionary (also known as a hash in some languages). It is key-value paired which means that the keys are in no particular order but each unique key is only tied to a single value (which can of course take the form of an array or another dictionary, but still counts as one value assigned to one key).

"Map" is a function that takes a list (such as the keys or values of "occupations") and returns an array of the original list with a function applied to it.

For example

(0..<4).map({$0 + 1})

Returns an array of [1, 2, 3, 4] because the $0 is Swift shorthand for "the value of the original array that am currently applying this function to.

  1. As mentioned in the answers above, when an item is added to a dictionary its position is unimportant, as the dictionary is not by nature sorted like an array would be.
-1
votes

To turn Eric and Luk's comments into an official answer:

Your var 'occupations' is a Swift Dictionary object.

A Dictionary in Swift (and Objective-C as well) does not have any order at all. It is an "unordered collection."As Luk says, it's a key-value store. You save and retrieve values using a key.

Your questions:

  1. Is var occupations a Map?

EDIT:

It's not called a Map in iOS, but it the equivalent of a map in other languages like C#, C++, and Java.

  1. In what order does new item be added?

Dictionaries do no have any order whatsoever.