4.2 Dictionaries

Dictionaries

  • use when you need to reference stored items with unique ID keys
  • no guarantee of order
  • keys and values are all fixed data types (types cannot change)
  • all values returned are optionals
  • can use all optional commands with dictionary items  (ie. ? ! if let)

Declaration

  • if let is used → dictionary is immutable (cannot change/add to it)
// declaration - create but don't initialize
var dict1: [String: Int]

// declaration - create/init with 0 value
var dict2: [String: Int] = [:]
var dict3 = [String: Int]()

// create and initialize with initial values
var dict4 = ["AZ":2, "AL":1]

Add & Change Items

// direct add -> NY, 2
dict3["NY"] = 5

// value changed -> AL, 3
dict4["AL"] = 3

// will insert or change (and return the old value)
dict4.updateValue(7, forKey: "MT")

Remove Items

// -> returns 2
dict4.removeValue(forKey: "AZ")

// will remove the pair -> no return
dict4["AL"] = nil

// entire dictionary cleared
dict4 = [:]

Iterating Over Items

// print keys and values -> key: MT, value: 7
for (key, value) in dict4 {
   print("key: \(key), value:\(value)")
}

// print keys -> key: MT
for key in dict4.keys {
   print("key: \(key)")
}

// print values -> values: 7
for value in dict4.values {
   print("values: \(value)")
}
  • key value → auto declared & type-inferred constants used to iterate so the item evaluated is not altered
  • no guaranteed order for dictionary loops

Miscellaneous

// referencing (no dot notation / unordered) -> optional 7
let state = dict4["MT"]

// count the # of items in the dictionary -> 1
dict4.count

// returns a Bool (true = empty)
dict4.isEmpty

Dictionaries pass by value

var dictA = [1: 1, 2: 2, 3: 3]
var dictB = dictA
dictB[3] = nil

// -> 1: 1, 2: 2, 3: 3
dictA
// -> 1: 1, 2: 2
dictB