Skip to content

Instantly share code, notes, and snippets.

@RinniSwift
Last active June 1, 2019 00:55
Show Gist options
  • Save RinniSwift/0af1d7148ba80d625236f6cc6997902c to your computer and use it in GitHub Desktop.
Save RinniSwift/0af1d7148ba80d625236f6cc6997902c to your computer and use it in GitHub Desktop.
var dict = ["blue": 2, "grey": 1, "purple": 4, "red": 2]
// subscript(key:)
dict["blue"] // Optional(2)
// subscript(key:default:)
dict["grey", default: 0] // 1
// subscript(position:)
if let ind = dict.firstIndex(where: { ($0.key == "red") }) {
print(dict[ind]) // (key: "red", value: 2)
}
if let indTwo = dict.index(forKey: "rin") {
print(dict[indTwo])
}
// first
dict.first // Optional((key: "purple", value: 4))
// randomElement()
dict.randomElement() // Optional((key: "grey", value: 1))
// keys and values
dict.keys // ["purple", "red", "grey", "blue"]
dict.values // [1, 4, 2, 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment