NSInvalidArgumentException,原因:' - [NSNull isEqualToString:]

时间:2017-08-06 08:07:14

标签: ios objective-c

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

  

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

这是我收到的字典

enter image description here

实施

-(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"]];
        }
}

1 个答案:

答案 0 :(得分:2)

您还需要检查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"]];
            }
        }

    }
}

希望这有帮助