gpt4 book ai didi

swift - 不能用字符串下标字典

转载 作者:可可西里 更新时间:2023-11-01 00:36:55 25 4
gpt4 key购买 nike

我编写了以下函数来访问 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)
}

但我无法访问 key ,因为我收到错误消息:

Cannot subscript a value of type 'Dictionary String,AnyObject> with an index of type 'String'

最佳答案

您正在尝试将 AnyObject 分配给标签的 text 属性。我会去:

func updateAddressLabel(notification: NSNotification) {
if let userInfo = notification.userInfo as? Dictionary<String,AnyObject>, let geocodeLocation = userInfo["geocodeLocation"] as? String {
infoLabelAddress.text = geocodeLocation
}
}

关于swift - 不能用字符串下标字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35528773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com