我收到推送通知并尝试按如下方式解析字典,但出现以下异常。
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull isEqualToString:]: unrecognized selector sent to instance 0x1a6574ef8'
这是我收到的字典
实现
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (application.applicationState == UIApplicationStateActive)
{
// exception happens the following line
if([[userInfo objectForKey:@"aps"] objectForKey:@"alert"] != NULL &&
[[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]isEqualToString:@"New Order!"])
{
[[NSNotificationCenter defaultCenter] postNotificationName: @"newOrderNotificationMessage" object: [userInfo objectForKey:@"aps"]];
}
}
最佳答案
你还需要检查 NSNull
类,试试这个
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (application.applicationState == UIApplicationStateActive)
{
// exception happens the following line
if([[userInfo objectForKey:@"aps"] objectForKey:@"alert"] != NULL && [[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] class] != [NSNull class]){
if ([[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]isEqualToString:@"New Order!"])
{
[[NSNotificationCenter defaultCenter] postNotificationName: @"newOrderNotificationMessage" object: [userInfo objectForKey:@"aps"]];
}
}
}
}
希望对你有帮助
关于ios - NSInvalidArgumentException,原因 : '-[NSNull isEqualToString:],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45529936/