Reputation: 3255
I wrote the following functions, to access a userInfo dictionary:
func updateAddressLabel(notification: NSNotification) {
let userInfo:Dictionary<String,AnyObject> = notification.userInfo as! Dictionary<String, AnyObject>
self.infoLabelAddress.text = userInfo["geocodeLocation"]
}
func updateDispatchInformation(notification: NSNotification) {
let userInfo:Dictionary<String,AnyObject> = notification.userInfo as! Dictionary<String, AnyObject>
let streetName = userInfo["streetName"]
let incidentLatitude = userInfo["latitude"]
let incidentLongitude = userInfo["longitude"]
// Set Dispatch Info Label
self.infoLabelTitle.text = "Notruf"
self.infoLabelAddress.text = streetName
self.createIncidentAnnotation(incidentLatitude, longitude: incidentLongitude)
}
But I cannot access the keys, as I get the errors:
Cannot subscript a value of type 'Dictionary String,AnyObject> with an index of type 'String'
Upvotes: 2
Views: 342
Reputation: 1786
You are trying to assign AnyObject
to label's text
property. I would go:
func updateAddressLabel(notification: NSNotification) {
if let userInfo = notification.userInfo as? Dictionary<String,AnyObject>, let geocodeLocation = userInfo["geocodeLocation"] as? String {
infoLabelAddress.text = geocodeLocation
}
}
Upvotes: 3
Reputation: 285072
userInfo
is Dictionary<String,AnyObject>
, to assign the value to a specific type like String
you have to downcast the object for example
self.infoLabelAddress.text = userInfo["geocodeLocation"] as! String
Upvotes: 2