2

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'

2 Answers 2

3

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.