Swift: Dictionary removeAtIndex Example
Xcode 7 beta 5 release notes mention Dictionary
's removeAtIndex
method now returns a key/value tuple pair rather than Void
. removeAtIndex
takes a parameter of type DictionaryIndex
which can be accessed using Dictionary
's indexForKey
method.
Example:
var aDictionary = [
"aKey" : "foo",
"anotherKey": "bar",
]
if let aDictionaryIndex = aDictionary.indexForKey("anotherKey") {
// This will remove the key/value pair from the dictionary and return it as a tuple pair.
let (key, value) = aDictionary.removeAtIndex(aDictionaryIndex)
print(key) // anotherKey
print(value) // bar
}