- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在 didreceiveRemoteNotification
中收到通知,但我无法将 userInfo 转换为 [String: Any] 类型的字典
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let dict = userInfo as! [String: Any]
if let response = dict["message"] as? [String: Any], let baseResponse = Mapper<NotificationModel>().map(JSON: response) {
//do some stuff
}
}
当我尝试将 dict["message"] 转换为! [String: Any] 错误发生,它说:
无法将类型“__NSCFString”(0x1cfa84f90)的值转换为“NSDictionary”(0x1cfa85bc0)。
这里是 dict["message"] 当我在控制台打印它时:
▿ Optional<Any>
- some : {"sender":
{"avatar_url":"http:\/\/api.moneyar.com\/APIs\/images\/15783070400.jpg","user_id":"15783","name":"mahdi moqadasi"}
,"conversation_id":"15783"
,"message_id":103597,
"time":1546778745,
"type":1,"message":"foo"
}
最佳答案
对于以下答案,代码未针对编译器进行测试,可能存在一些可以轻松修复的拼写错误,其中一些是故意完成的以体现其背后的逻辑,而不是添加 if let
/guard let
、as?
等是必需的,但会在解释中增加噪音。
不再重复@vadian answer ,这是正确的,并解释了失败的原因。
所以我们很清楚 dict["message"]
是一个 String
。
您在 JSON 首字母缩略词中似乎缺少的一条信息是代表“N”的内容:表示法。
当您打印 dict["message"]
时,您并没有真正的键/值对象,您有一个表示键值对象的字符串,但不是 Swift 表示形式。您打印了 JSON Stringified(因为它显然比十六进制数据 JSON 更具可读性)。如果在回答后打印 jsonDict
,您会发现输出结构可能不同。
因此,一如既往,您的基本工具是:
Data <== data(encoding:)/init(data:encoding:) ==> String
Data <== jsonObject(with:options:)/data(withJSONObject:options:) ==> Array or Dictionary //I bypass voluntarily the specific case of String at top level
那我们开始吧!
let jsonStringifiedString = dict["message"] as String
let jsonStringifiedData = jsonStringifiedString.data(using: .utf8) as Data
let jsonDict = try JSONSerialization.jsonObject(with: jsonStringifiedData, options: []) as [String: Any]
let baseResponse = Mapper<NotificationModel>().map(JSON: jsonDict)
如果我是你,我会研究 Mapper
,如果没有办法做类似的事情:
let baseResponse = Mapper<NotificationModel>().map(JSONData: jsonStringifiedData)
或
let baseResponse = Mapper<NotificationModel>().map(JSONString: jsonStringifiedString)
因为有时在 JSON 中嵌入了 JSONStringified,您可能需要在 String
或 Data
上直接调用它。或者只是因为基本的 URLSession
请求在其闭包中返回了一个 Data
对象,而您想直接使用它。
关于swift - 转换 userInfo [AnyHashable : Any] to [String: Any],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54061639/
我是 Swift 的新手,我不知道如何通过索引访问 [AnyHashable : Any] 类型的变量 更具体地说,我想通过索引访问 HTTPURLResponse 实例的 allHeaderFiel
我有这个 Playground : import Foundation enum WeekDay: Int { case mon, tues, wed, thurs, fri, sat, su
我正在将我的 Objective-C 代码转换为 Swift。我有自己的 DataStructure 类,称为“DCclass”,继承自“NSObject”...我已经完成了所有必需的事情,例如:在
我需要比较符合协议(protocol) P 的结构数组。 P 不能符合 Equatable,因为它不能有“ self 要求”。 正在创建 AnyEquatable作为类型删除是为此目的的常见做法。但是
抱歉, super 菜鸟试图编译这个,这是我试图修复的最后一个错误。 let optionalString0 = formatter.stringFromNumber(NSNumber(localN
这是一个 AnyHashable 不支持枚举转换的简单案例。 enum testEnum: String { case Test } let myObject: AnyHashable = t
我正在将我的代码从 Objective C 转换为 Swift。我有一个代码,它从一个 swift 方法调用一个 objective-c 方法。 obj C 方法返回一个 NSDictionary。显
我在 didreceiveRemoteNotification 中收到通知,但我无法将 userInfo 转换为 [String: Any] 类型的字典 func application(_ appl
我有一个由 XCDYoutubeKit pod 在 swift projet 中返回的字典数组: print(video?.streamURLs ?? "nothing to print") [Any
我怎么能做到这一点? var dict = [AnyHashable : Int]() dict[NSObject()] = 1 dict[""] = 2 这意味着 NSObj
我正在尝试解析推送通知中的next_question_detail,但全部失败: for data in notification.request.content.userInfo { p
我尝试使用 Snapchat 的登录工具包来允许用户使用 Snapchat 登录,但他们的文档给了我一个错误。 复制文档后会出现“Type '[AnyHashable : Any]?'”没有下标成员。
我正在尝试从类型为 [AnyHashable: Any] 的变量访问特定值,但是,我在访问任何值时遇到错误。我已经浏览了有关此问题的互联网,但没有找到任何具体的解决方案。那么有没有其他方法可以访问它们
我有以下对象,然后想合并。 var prop1 = [AnyHashable: Any]() prop1["key1"] = "Phase3" prop1["key2"] = "405"
我怎么可以这样做? var dict = [AnyHashable : Int]() dict[NSObject()] = 1 dict[""] = 2 这意味着 NSObje
我正在调用 Web 服务,它返回的数据是 [AnyHashable:URL] 类型的字典。对于 key ,我确切地知道它的类型,即 String。所以我尝试使用以下方法获取 URL: streamUR
我正在使用 APAddressBook从用户手机获取地址簿。当我打印变量 serializedContacts 时,它会生成以下用户对象: [[ AnyHashable("name"): {
我通过大量教程摸索着理解上述 3 个术语之间的区别,并找到了新术语 type erased container,现在它让我感到困惑。这引发了很多问题。 为什么 Swift 引入 AnyHashable
如何在 swift 中访问任何 AnyHashable 数据我的数据如下,下面是我单击通知时的日志。当我单击 notif 以及数据记录时,我想打印或获取它,以便我可以将其加载到 View 上。它已经在
我目前正在使用 Swift 4 开发电子商务应用程序并将数据与 Moltin 关联起来。我正在为学校做这件事,所以我是新来的。我也在关注 CodeWithChris 的教程,但那已经过时并且我的布局有
我是一名优秀的程序员,十分优秀!