我正在将我的项目升级到 swift 3.0,但在 userInfo["aps"] 上出现错误。我试过在它之间加上问号和解释点,但没有成功
private func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
if let aps = userInfo["aps"] as? NSDictionary {
if let alert = aps["alert"] as? NSDictionary {
if let message = alert["message"] as? NSString {
//Do stuff
}
} else if let alert = aps["alert"] as? NSString {
//Do stuff
}
}
}
最佳答案
与往常一样,使用 Swift 集合类型,Foundation 集合类型不提供有关其内容的任何类型信息。
首先使用委托(delegate)方法的实际签名,将方法标记为私有(private)并保留旧签名是一个糟糕的妥协。但是请考虑该方法已被弃用。
func application(_ application: UIApplication, didReceiveRemoteNotification
userInfo: [AnyHashable : Any])
JSON
集合类型总是[String:Any]
(字典)或[[String:Any]]
(数组)
func application(_ application: UIApplication, didReceiveRemoteNotification
userInfo: [AnyHashable : Any])
if let aps = userInfo["aps"] as? [String:Any] {
if let alert = aps["alert"] as? [String:Any] {
if let message = alert["message"] as? String {
//Do stuff
}
} else if let alert = aps["alert"] as? String {
//Do stuff
}
}
}
关于ios - 对具有字符串文字和 NSObject 下标类型的下标的引用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39730781/