如何通过 Swift 4 中的发件人检索用户数据?
How do you retrieve userData through a sender in Swift 4?
我似乎无法在不收到错误的情况下检索已发送的发件人数据 "Value of 'Any' has no subscripts"。
这似乎是一个新错误,因为我一直都是这样做的,而且我找不到有关如何修复它的信息。
let selector = #selector(self.updatePoint(sender:))
Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: selector,
userInfo: ["index": 3, "tempPoint": tempPoints[3]],
repeats: false)
@objc func updatePoint(sender: Timer) {
guard let index = sender.userInfo?["index"] as? Int else {return} // Error: Value of 'Any' has no subscripts
...
}
userInfo
是类型 Any
你需要先把它转换成 [String:Any]
guard let info = sender.userInfo as? [String:Any],let index = info["index"] as? Int else {return}
我似乎无法在不收到错误的情况下检索已发送的发件人数据 "Value of 'Any' has no subscripts"。
这似乎是一个新错误,因为我一直都是这样做的,而且我找不到有关如何修复它的信息。
let selector = #selector(self.updatePoint(sender:))
Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: selector,
userInfo: ["index": 3, "tempPoint": tempPoints[3]],
repeats: false)
@objc func updatePoint(sender: Timer) {
guard let index = sender.userInfo?["index"] as? Int else {return} // Error: Value of 'Any' has no subscripts
...
}
userInfo
是类型 Any
你需要先把它转换成 [String:Any]
guard let info = sender.userInfo as? [String:Any],let index = info["index"] as? Int else {return}