Skip to content

Instantly share code, notes, and snippets.

@deathlezz
Created October 17, 2021 07:30
Show Gist options
  • Save deathlezz/fbc82605618496cfca5c71d788f104c7 to your computer and use it in GitHub Desktop.
Save deathlezz/fbc82605618496cfca5c71d788f104c7 to your computer and use it in GitHub Desktop.
Simple use of dictionaries in Swift 5.
//
// Simple use of dictionaries
//
// basic dictionary
var age = [
"Jack": 23,
"Alice": 25
]
age["Mark"] = 45
print(age["Mark"]!) // 45
print(age) // ["Mark": 45, "Alice": 25, "Jack": 23]
age.removeValue(forKey: "Jack")
print(age) // ["Alice": 25, "Mark": 45]
// default values
var user = [
"Jack": "Sparrow",
"Will": "Turner"
]
print(user["Hector", default: "Barbossa"]) // Barbossa
// empty collection
var weight = [String: Double]()
weight["Richard"] = 150
print(weight) // ["Richard": 150.0]
print(weight["Richard"]!) // 150.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment