我编写了以下函数来访问userInfo字典:
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)
}
但我无法访问密钥,因为我收到错误:
无法下标类型的值&#39; Dictionary String,AnyObject&gt;索引类型为&#39; String&#39;
答案 0 :(得分:3)
您正在尝试将AnyObject
分配给标签text
媒体资源。我会去:
func updateAddressLabel(notification: NSNotification) {
if let userInfo = notification.userInfo as? Dictionary<String,AnyObject>, let geocodeLocation = userInfo["geocodeLocation"] as? String {
infoLabelAddress.text = geocodeLocation
}
}
答案 1 :(得分:2)
userInfo
为Dictionary<String,AnyObject>
,要将值分配给特定类型,例如String
,您必须向下转换对象,例如
self.infoLabelAddress.text = userInfo["geocodeLocation"] as! String