【发布时间】:2019-06-01 12:09:51
【问题描述】:
我正在尝试解析 gcm.notification.createdAt 键,但它的值没有被转换为 Int。
奇怪的是,即使值类型显然是Int,它也不起作用,正如您所看到的。
知道我在这里做错了什么吗?
userInfo is [AnyHashable("gcm.notification.chatUID"): -LgHYXKFNmP-mQo7s9nB,
AnyHashable("gcm.notification.type"): chat,
AnyHashable("gcm.notification.createdAt"): 1559389303,
AnyHashable("google.c.a.e"): 1,
AnyHashable("gcm.message_id"): 0:1559389316529351%e413fc3ee413fc3e,
AnyHashable("aps"): {
alert = {
body = "You have a new message";
title = "New Message from Lehz Raus";
};
badge = 1;
sound = default;
}]
let userInfo = response.notification.request.content.userInfo
guard let createdAt = userInfo["gcm.notification.createdAt"] as? Int else {
print("gcm.notification.createdAt is not showing")
return
}
//this works as expected
guard let chatUUUUID = userInfo["gcm.notification.chatUID"] as? String else {
print("no chatUUUUID printed")
return
}
【问题讨论】:
-
print(type(of: userInfo["gcm.notification.createdAt"]))打印什么? -
试试
userInfo[AnyHashable("gcm.notification.createdAt")] -
@vacawama 它打印
Optional<Any> (lldb) -
好的,打开它:
print(type(of: userInfo["gcm.notification.createdAt"]!))打印什么? -
@vacawama ` __NSCFString (lldb) ` 从服务器发送它时,它看起来像转换为字符串。我不知道为什么,因为我已经检查了三倍,并且在服务器上它作为 Int 发送