- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从类型为 [AnyHashable: Any] 的变量访问特定值,但是,我在访问任何值时遇到错误。我已经浏览了有关此问题的互联网,但没有找到任何具体的解决方案。那么有没有其他方法可以访问它们?请帮助并提前致谢。
我遇到错误的函数
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)
}
获取打印用户信息的数据
[AnyHashable("gcm.notification.data"): {"filters":"fjjnbbx zjbxzj","history":"dsfdxf","message":"value1","source":"message source"}]
最佳答案
我解决了这个问题,因为我收到的信息是 NSString(字符串中的 JSON)类型,而不是 NSDictionary/[String : Any]。我在下面提供了解决我的问题的工作代码 -
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)")
}
}
}
关于ios - 无法访问 [AnyHashable :Any] - Swift 4 类型的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51472321/
我是 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 的教程,但那已经过时并且我的布局有
我是一名优秀的程序员,十分优秀!