问题描述
我有一个来自 firebase 的字典数组,如下所示:
"gcm.notification.data" = "{\"request\":\"update_location\",\"latitude\":\"45.48945419494574\",\"customMessage\":{\"loc-args\":[\"iPhone di Tester\"],\"loc-key\":\"LOCATION_CHECKIN\"},\"type\":\"checkin\",\"message\":\"Ggg\",\"经度\":\"9.195329826333742\",\"child\":{\"name\":\"iPhone di Tester\",\"pid\":\"C312EDDC-E8A8-4EFC-9E65-957BE5DAC5FC\"}}";
我试图解开请求,如下所示,但它崩溃了,谁能帮忙.
NSDictionary *gcmnotificationdat = [userInfo objectForKey:@"gcm.notification.data"];NSString *request = [gcmnotificationdat objectForKey:@"request"];
您遇到了崩溃.作为开发人员,阅读它很重要.你看懂也可以不看,但是你肯定可以去找,这个是比较有名的.这是任何 iOS 开发人员都应该知道的基本崩溃消息.如果没有,请与我们分享.您将有更好的机会获得答案.
最重要的部分是:
-[__NSCFConstantString objectForKey:]:无法识别的选择器发送到实例 0x100002090
这意味着您正在尝试在 NSString
对象上调用方法 objectForKey:
.NSString
不知道,这就是它崩溃的原因.
key gcm.notification.data
的值是一个 JSON Stringified.
所以 [userInfo objectForKey:@"gcm.notification.data"];
实际上是一个 NSString
而不是 NSDictionary
.>
让我们现在修复它:
//为了测试而创建样本NSDictionary *userInfo = @{@"gcm.notification.data": @"{\"request\":\"update_location\",\"latitude\":\"45.48945419494574\",\"customMessage\":{\"loc-args\":[\"iPhone di Tester\"],\"loc-key\":\"LOCATION_CHECKIN\"},\"type\":\"checkin\",\"message\":\"Ggg\",\"longitude\":\"9.195329826333742\",\"child\":{\"name\":\"iPhone di Tester\",\"pid\":\"C312EDDC-E8A8-4EFC-9E65-957BE5DAC5FC\"}}"};//解析NSString *gcmNotificationJSONString = [userInfo objectForKey:@"gcm.notification.data"];NSData *gcmNotificationJSONData = [gcmNotificationJSONString dataUsingEncoding:NSUTF8StringEncoding];NSDictionary *gcmNotification = [NSJSONSerialization JSONObjectWithData:gcmNotificationJSONData options:0 error:nil];NSString *request = [gcmNotification objectForKey:@"request"];NSLog(@"请求:%@", request);
注意:我删除了您使用的 var 名称的dat"部分,因为键以data"结尾,以免混淆 NSString
、NSData
和 NSDictionary
因为我从类中明确命名它们.我在评论中打错了一个类,小心并从这里的代码中修复它.
I have an array of dictionary from firebase like bellow :
"gcm.notification.data" = "{\"request\":\"update_location\",\"latitude\":\"45.48945419494574\",\"customMessage\":{\"loc-args\":[\"iPhone di Tester\"],\"loc-key\":\"LOCATION_CHECKIN\"},\"type\":\"checkin\",\"message\":\"Ggg\",\"longitude\":\"9.195329826333742\",\"child\":{\"name\":\"iPhone di Tester\",\"pid\":\"C312EDDC-E8A8-4EFC-9E65-957BE5DAC5FC\"}}";
I tried to unwrap the request, like bellow but it crash, can anyone help.
NSDictionary *gcmnotificationdat = [userInfo objectForKey:@"gcm.notification.data"];
NSString *request = [gcmnotificationdat objectForKey:@"request"];
You got a crash. As a developer it's important to read it. You can either understand it or not, but you can certainly look for it, and this one is quite known. It's a basic crash message that any iOS developer should know.If you don't, share it with us. You'll get better chances to get answers.
The most important part being:
-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x100002090
It means that you are trying to call a method objectForKey:
on a NSString
object. NSString
doesn't know it, and that's why it crashes.
The value of key gcm.notification.data
is a JSON Stringified.
So [userInfo objectForKey:@"gcm.notification.data"];
is in fact a NSString
not a NSDictionary
.
Let's fix it now:
//Creation of the sample for the sake of the test
NSDictionary *userInfo = @{@"gcm.notification.data": @"{\"request\":\"update_location\",\"latitude\":\"45.48945419494574\",\"customMessage\":{\"loc-args\":[\"iPhone di Tester\"],\"loc-key\":\"LOCATION_CHECKIN\"},\"type\":\"checkin\",\"message\":\"Ggg\",\"longitude\":\"9.195329826333742\",\"child\":{\"name\":\"iPhone di Tester\",\"pid\":\"C312EDDC-E8A8-4EFC-9E65-957BE5DAC5FC\"}}"};
//Parsing
NSString *gcmNotificationJSONString = [userInfo objectForKey:@"gcm.notification.data"];
NSData *gcmNotificationJSONData = [gcmNotificationJSONString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *gcmNotification = [NSJSONSerialization JSONObjectWithData:gcmNotificationJSONData options:0 error:nil];
NSString *request = [gcmNotification objectForKey:@"request"];
NSLog(@"Request: %@", request);
Note: I removed the "dat" part of the var names, that you used because the key ends with "data" to not confuse NSString
, NSData
and NSDictionary
as I explicitly named them from the class. I mistype a class in the comment, be careful and fix it from the code here.
这篇关于展开字典数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!