Swift 如何从userInfo通知强制转换为Int对象?

Swift 如何从userInfo通知强制转换为Int对象?,swift,casting,Swift,Casting,我试图解析gcm.notification.createdAt键,但它的值没有被转换为Int。 奇怪的是,即使您可以看到值类型显然是Int,它也不起作用。 知道我做错了什么吗 userInfo is [AnyHashable("gcm.notification.chatUID"): -LgHYXKFNmP-mQo7s9nB, AnyHashable("gcm.notification.type"): chat, AnyHashable("g

我试图解析
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
    }

我怀疑您的值不是真正的
Int

您可以通过打印来调查基础类型:

print(type(of: userInfo["gcm.notification.createdAt"]!))
从评论中,您说它返回:
\uu NSCFString
,所以服务器给您一个
字符串。您可以在
guard
语句中添加一行,将其转换为
Int

guard let createdAt = userInfo["gcm.notification.createdAt"] as? String,
      let createdAtInt = Int(createdAt) else {
    print("gcm.notification.createdAt is not showing")
    return
}

什么是
print(类型:userInfo[“gcm.notification.createdAt”)
print?试试
userInfo[AnyHashable(“gcm.notification.createdAt”)]
@vacawama it prints
可选(lldb)
Ok,打开它:什么是
打印的(类型:userInfo[“gcm.notification.createdAt”!)
print?@vacawama`\u NSCFString(lldb)`从服务器发送时,它看起来像是转换成字符串。我不知道为什么,因为我已经检查了三倍,在服务器上它被作为Int发送