【问题标题】:NSInvalidArgumentException, reason: '-[NSNull isEqualToString:]NSInvalidArgumentException,原因:'-[NSNull isEqualToString:]
【发布时间】:2017-08-06 08:07:13
【问题描述】:

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

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSNull isEqualToString:]: 无法识别的选择器发送到实例 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 类

标签: ios objective-c


【解决方案1】:

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

    }
}

希望对你有帮助

【讨论】:

猜你喜欢
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多