iOS报错-[NSNull intValue]: unrecognized selector sent to

自己测试出了自己的一个小bug,趁还没打包测试,赶紧自己改改~~

报错提示:

2019-12-23 09:47:23.005740+0800 E源充[416:30434] -[NSNull intValue]: unrecognized selector sent to instance 0x249ded7f0
2019-12-23 09:47:23.006919+0800 E源充[416:30434] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull intValue]: unrecognized selector sent to instance 0x249ded7f0'
*** First throw call stack:
(0x21968e98c 0x2188679f8 0x2195ab1c8 0x2196941d4 0x219695e6c 0x1008f81fc 0x1008e3670 0x100939988 0x1009597f4 0x100fcb6f4 0x100fccc78 0x100fda49c 0x21962032c 0x21961b264 0x21961a7c0 0x21b81b79c 0x245fd7c38 0x1008ad7f8 0x2190de8e0)
libc++abi.dylib: terminating with uncaught exception of type NSException

因为找到了必现条件,事情就简单多了。

看了一遍报错提示,大概意思就是对一个空值进行了intValue处理,具体也没定位到哪个位置。

笨办法走起:

全局搜索 intValue,将所有的intValue打上断点~~~

找到了每一个intValue的执行位置,查看具体的值:

2.png

这里是空值,然后再查看status值的来源,是从接口请求过来的数据data里面的orderStatus,打印字典:

(lldb) po responseObject
{
  "errors" : null,
  "data" : {
    "pileEntp" : null,
    "stationName" : null,
    "gunType" : null,
    "orderStatus" : null,
    "pileAccessToken" : null,
    "pileCode" : null,
    "preinstallChargeEnergy" : null,
    "thirdpartyOrderNo" : null,
    "userPhone" : null,
    "entpId" : null,
    "orderType" : null,
    "gunCode" : null,
    "thirdpartyOrderId" : null,
    "id" : null,
    "orderNo" : null,
    "carNo" : null,
    "gunId" : null,
    "cityCode" : null,
    "stationId" : null,
    "chargeType" : null,
    "createTime" : null,
    "pileId" : null,
    "preinstallChargeMoney" : null,
    "userId" : null
  },
  "exceptionName" : null,
  "message" : "success",
  "status" : 200
}

这样只能自己进行判空操作然后处理了,常规处理方法~

  if ([self rq_isEmpty:status]) {
  return;
  }

处理方式代码如下,这里使用的是一个一个工具类,为方便阅读,就直接改成方法来调用了

- (BOOL) rq_isEmpty:(NSString*)str
{
    if (str == nil) {
        return TRUE;
    }
    
    if ([str isEqual:[NSNull null]]) {
        return TRUE;
    }
    
    if ([str isEqual:@""]) {
        return TRUE;
    }
    
    if ([str isEqual:@"(null)"]) {
        return TRUE;
    }
    
    
    if ([str isKindOfClass:[NSString class]]) {
        
        if (str.length == 0) {
            return TRUE;
        }
        
        NSString *str1 = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        
        if ([str1 isEqual:@""]) {
            return TRUE;
        }
    }
    return FALSE;
}

你可能感兴趣的:(iOS报错-[NSNull intValue]: unrecognized selector sent to)