简体   繁体   English

无法访问[AnyHashable:Any]类型的值-Swift 4

[英]Unable to access values of type [AnyHashable:Any] - Swift 4

I am trying to access a particular value from a variable that is of type [AnyHashable: Any] however, I am getting an error while accessing any value. 我试图从类型为[AnyHashable:Any]的变量访问特定值,但是在访问任何值时遇到错误。 I have browsed the internet regarding this issue, but I did not get any specific solution for it. 我已经浏览了有关此问题的互联网,但没有得到任何具体的解决方案。 So is there any other way to access them? 那么还有其他方法可以访问它们吗? Please help and thanks in advance. 请帮助并提前致谢。

Function where I am getting an error 我遇到错误的功能

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }
        //Print full message
        print(userInfo)

        guard let aps = userInfo[AnyHashable("gcm.notification.data")],
            let data = aps["filters"] as! String else { // Getting the error here
                return
        }
        print(aps)
        completionHandler(UIBackgroundFetchResult.newData)
    }

Fetched data on printing the userInfo 打印用户信息时获取的数据

[AnyHashable("gcm.notification.data"): {"filters":"fjjnbbx zjbxzj","history":"dsfdxf","message":"value1","source":"message source"}]

Get the data from [AnyHashable("gcm.notification.data")] 从[AnyHashable(“ gcm.notification.data”)]获取数据

if let aps = userInfo["aps"] as? NSDictionary, let notificationData = userInfo["gcm.notification.data"] as? NSString {

          var dictonary:NSDictionary?
            if let data = notificationData.data(using: String.Encoding.utf8.rawValue) {
                do {
                    dictonary = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary

                    if let alertDictionary = dictonary {
                        print(alertDictionary)
                    }
                } catch{
                    print(error)
                }

I resolved the issue as the information I was receiving was of type NSString (JSON in String) not NSDictionary / [String : Any]. 我解决了这个问题,因为我收到的信息是NSString类型(String中的JSON),而不是NSDictionary / [String:Any]。 I have provided the working code that solved my problem below - 我在下面提供了解决我的问题的工作代码-

if let notificationData = userInfo["gcm.notification.data"] as? NSString {
                var dictionary : NSDictionary?
                if let data = notificationData.data(using: String.Encoding.utf8.rawValue) {
                    do {
                        dictionary = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary
                        print("DICTIONARY : \(dictionary)")
                        if let dict = dictionary {
                            print(dict["alert_message"] as? String)
                            // ..... and so on
                        }
                    } catch let error as NSError {
                        print("Error: \(error)")
                    }
                }
            } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 swift 3.0如何在Swift 3中的`Any`中访问`AnyHashable`类型? - swift 3.0 How can I access `AnyHashable` types in `Any` in Swift 3? 错误:键入[AnyHashable:任何]? 没有下标成员(Swift) - Error: Type [AnyHashable: Any]? has no subscript members (Swift) swift ios 如何快速访问 AnyHashable 数据 - swift ios how to access AnyHashable data in swift Swift 3中Any,Hashable,AnyHashable有什么区别? - What is difference between Any , Hashable , AnyHashable in Swift 3? 无法将类型“[UserModel]”的值转换为预期的参数类型“[AnyHashable: Any]?” - Cannot convert value of type '[UserModel]' to expected argument type '[AnyHashable : Any]?' 条件绑定的初始值设定项必须具有 Optional 类型,而不是“[AnyHashable : Any]” - Initializer for conditional binding must have Optional type, not '[AnyHashable : Any]' 二元运算符“==”不能应用于“[AnyHashable: Any]?”类型的操作数和“字符串” - Binary operator '==' cannot be applied to operands of type '[AnyHashable : Any]?' and 'String' 如何从IOS Swift'Any'类型访问和获取嵌套值? - How to access & get nested values from IOS Swift 'Any' type? 输入“ [[AnyHashable:任何]”? 没有下标成员-使用Moltin API - Type '[AnyHashable : Any]?' has no subscript members - Using Moltin API AnyHashable:通过推送通知接收时,任何内容都不会迅速转换为字典 - AnyHashable:Any doesn't convert to dictionary in swift when receives through push notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM