Swift 无法强制转换类型为\uu NSCFString的值
我正在试用FCM消息服务。我把一切都安排好了。当我收到来自FCM的消息时,我得到以下错误。我不确定这里出了什么问题:Swift 无法强制转换类型为\uu NSCFString的值,swift,swift3,push-notification,firebase-cloud-messaging,Swift,Swift3,Push Notification,Firebase Cloud Messaging,我正在试用FCM消息服务。我把一切都安排好了。当我收到来自FCM的消息时,我得到以下错误。我不确定这里出了什么问题: AnyHashable("google.c.a.c_id"): 1586592282379086000, AnyHashable("gcm.message_id"): 0:1515418580276498%f4373656f4373656] Could not cast value of type '__NSCFString' (0x1b4c70040) to 'NSDicti
AnyHashable("google.c.a.c_id"): 1586592282379086000, AnyHashable("gcm.message_id"): 0:1515418580276498%f4373656f4373656]
Could not cast value of type '__NSCFString' (0x1b4c70040) to 'NSDictionary' (0x1b4c70b80).
2018-01-08 19:06:21.198548+0530 Ozone[1067:540436] Could not cast value of type '__NSCFString' (0x1b4c70040) to 'NSDictionary' (0x1b4c70b80).
奇怪的是,我创建了一个单独的推送通知应用程序,并把相同的代码,它是工作在那里。这里可能出了什么问题
下面是代码:我在let d:[String:Any]……行中得到了错误
UNNotificationPresentationOptions) -> Void) {
print("Handle push from foreground\(notification.request.content.userInfo)")
let dict = notification.request.content.userInfo["aps"] as! NSDictionary
let d : [String : Any] = dict["alert"] as! [String : Any]
let body : String = d["body"] as! String
let title : String = d["title"] as! String
print("Title:\(title) + body:\(body)")
self.showAlertAppDelegate(title: title,message:body,buttonTitle:"ok",window:self.window!)
}
有人能帮我吗?我知道这是一个非常普遍的错误,但不确定为什么会出现这种情况,因为它在另一个应用程序中运行良好
谢谢
警报
键可以是字符串或字典(取决于您的服务器)。你可以试试这个
if let dict = notification.request.content.userInfo["aps"] as? [String : Any] {
if let d = dict["alert"] as? [String : Any],
let body = d["body"] as? String,
let title = d["title"] as? String {
print("Title:\(title) + body:\(body)")
} else if let body = dict["alert"] as? String {
print("body:\(body)")
}
}
此外,您还应避免强制施放,不要在swift中使用
NSDictionary
dict[“alert”]
在这里是String
,而不是[String:Any]
。检查。另外,不要到处使用强制转换。@user28434那么我应该使用什么呢?如果我将其更改为string,我得到:不能用string类型的索引为string类型的值下标,这是因为您可以用string
下标string
。你应该看看@Bilal关于分支类型的答案。这是一个问题。我很惊讶相同的代码在不同的项目中工作得很好。是否可以在aps中添加更多参数?