我收到推送通知,并尝试按以下方式解析字典,但出现以下异常。


  ***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[NSNull isEqualToString:]:
  无法识别的选择器已发送到实例0x1a6574ef8'


这是我收到的字典

ios - NSInvalidArgumentException,原因:“-[NSNull isEqualToString:]-LMLPHP

实作

-(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/

10-13 04:14