I can't find in the documentation how to reference a value in an object of type Dictionary using object notation instead of subscript notation.
Anybody know how to do that?
I can't find in the documentation how to reference a value in an object of type Dictionary using object notation instead of subscript notation.
Anybody know how to do that?
Is updateValue(_:forKey:) what you are looking for :
« As an alternative to subscripting, use a dictionary’s updateValue(_:forKey:) method to set or update the value for a particular key. Like the subscript examples above, the updateValue(_:forKey:) method sets a value for a key if none exists, or updates the value if that key already exists. Unlike a subscript, however, the updateValue(_:forKey:) method returns the old value after performing an update. This enables you to check whether or not an update took place.
The updateValue(_:forKey:) method returns an optional value of the dictionary’s value type. For a dictionary that stores String values, for example, the method returns a value of type String?, or “optional String”. This optional value contains the old value for that key if one existed before the update, or nil if no value existed:
if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") {
print("The old value for DUB was \(oldValue).")
}
From: Apple Inc. « The Swift Programming Language (Swift 3). » iBooks.
I found the documentation for updateValue. I should have been more clear. I'm trying to retrueve a value. In any case, thanks.