Skip to content

Latest commit

 

History

History
132 lines (76 loc) · 1.88 KB

File metadata and controls

132 lines (76 loc) · 1.88 KB

Basics | Arrays | Colors | Date and Time | Dictionaries | Sets | Sorting | Strings | Functional Swift

Swift Dictionaries/Hash Maps

Dictionary Creation

var dictionary:[String:String] = [:] // Empty

dictionary = ["AAPL": "Apple",
              "MSFT": "Microsoft",
              "AMZN": "Amazon",
              "GOOG": "Google"]

let sharesOwned = ["AAPL": 200, "MSFT": 50] // [String:Int]

Number of Elements (Keys) in Dictionary

let n = dictionary.count

Print value. Should wrap in let. See below

let appleLongName = dictionary["AAPL"]!

print("\(appleLongName)")

Iterate over dictionary keys and values

func dumpDictionary() {

    for (key, value) in dictionary {
        print("\(key), \(value)")
    }
}

dumpDictionary()

Does Dictionary contain a key?

Value might not exist so we wrap in let

if let value = dictionary["AAPL"] {
    print("Found value for key = \(value)")
} else {
    dictionary["AAPL"] = "Apple" // Add
}
if dictionary["AAPL"] == nil {
   dictionary["AAPL"] = "Apple"
}

Add New Key/Value

dictionary["GOOGL"] = "Alphabet"

Get all keys

dictionary.keys

Remove Key/Value

dictionary.removeValue(forKey: "GOOG")

dictionary["AAPL"] = nil

dictionary.count

let sortedKeys =  Array(dictionary.keys).sorted(by: <)

Print, sorted by keys

for (key, value) in dictionary.sorted(by: { $0.0 < $1.0 }) {

    //    let value = dictionary[key]

    print("=>\(key), \(value)")

}
dictionary.updateValue("Alpha", forKey: "ALPHA")

dumpDictionary()

Remove All Dictionary Values

dictionary.removeAll()

Invert Key/Values