NSNull boolValue 获取崩溃目标 c



崩溃 :

[NSNull boolValue]:发送到实例0x1b5b12878的无法识别的选择器 2018-05-13 19:04:46.024492+0530 医生快递提供商[1681:579933] * 由于未捕获的异常"NSInvalidArgumentException"而终止应用程序,原因:"-[NSNull boolValue]:发送到实例0x1b5b12878的无法识别的选择器

法典:

NSUserDefaults *pref=[NSUserDefaults standardUserDefaults];
strUserId = [pref valueForKey:PARAM_ID];
strUserToken = [pref valueForKey:PARAM_TOKEN];
NSMutableDictionary *dictparam=[[NSMutableDictionary alloc]init];
[dictparam setObject:strUserId forKey:PARAM_ID];
[dictparam setObject:strUserToken forKey:PARAM_TOKEN];
[dictparam setObject:struser_longi forKey:PARAM_LONGITUDE];
[dictparam setObject:struser_lati forKey:PARAM_LATITUDE];
[dictparam setObject:strRequsetId forKey:PARAM_REQUEST_ID];
[dictparam setObject:@"0" forKey:PARAM_DISTANCE];
AFNHelper *afn=[[AFNHelper alloc]initWithRequestMethod:POST_METHOD];
[afn getDataFromPath:FILE_WALK_LOCATION withParamData:dictparam withBlock:^(id response, NSError *error)
{
NSLog(@"Update Walk Location = %@",response);
if (response)
{
payment=[[response valueForKey:@"payment_type"] boolValue];
NSLog(@"Something To Print");
NSLog(@"%d", payment);
NSLog(@"Something To Print");
if (payment==0)
{
self.lblPayment.text = NSLocalizedString(@"CARD", nil);
self.imgCashCard.image = [UIImage imageNamed:@"card"];
}
else if (payment==1)
{
self.lblPayment.text = NSLocalizedString(@"CASH", nil);
self.imgCashCard.image = [UIImage imageNamed:@"cash"];
}
if([[response valueForKey:@"success"] intValue]==1)
{
totalDist=[[response valueForKey:@"distance"]floatValue];
[self.btnDistance setTitle:[NSString stringWithFormat:@"%.2f %@",[[response valueForKey:@"distance"] floatValue],[response valueForKey:@"unit"]] forState:UIControlStateNormal];
}
else
{
[self.btnDistance setTitle:[NSString stringWithFormat:@"%.2f %@",[[response valueForKey:@"distance"] floatValue],[response valueForKey:@"unit"]] forState:UIControlStateNormal];
if ([[response valueForKey:@"is_cancelled"] integerValue]==1)
{
[self.timerForCancelRequest invalidate];
[self.timerForDestinationAddr invalidate];
NSUserDefaults *pref=[NSUserDefaults standardUserDefaults];
[pref removeObjectForKey:PREF_REQUEST_ID];
[pref removeObjectForKey:PREF_NAV];
strRequsetId=[pref valueForKey:PREF_REQUEST_ID];

[APPDELEGATE showToastMessage:NSLocalizedString(@"Request Canceled", nil) ];

is_walker_started=0;
is_walker_arrived=0;
is_started=0;
[btnWalker setHidden:NO];
[btnArrived setHidden:YES];
[btnWalk setHidden:YES];
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
}
}];

此行上的错误:

NSLog(@"Update Walk Location = %@",response);

"dest_latitude" = 0;">
"dest_longitude" = 0;
错误 ="服务尚未启动";
"error_code" = 414;
"is_cancelled" = 0;
"payment_type" = ";
成功 = 0;
单位 = 公里;

获取 payment_type 的空值...但是在这条线上崩溃

payment=[[response valueForKey:@"payment_type"] boolValue];

点知道如何处理它。但是这个打印函数本身不打印:

NSLog(@"Something To Print");
NSLog(@"%d", payment);
NSLog(@"Something To Print");

关于如何处理这个问题的任何想法?

更新:

if([response valueForKey:@"payment_type"] != nil || ![[response valueForKey:@"payment_type"] isEqualToString:@""]){
payment=[[response valueForKey:@"payment_type"] boolValue];
NSLog(@"Something To Print");
NSLog(@"%d", payment);
NSLog(@"Something To Print");
if (payment==0)
{
self.lblPayment.text = NSLocalizedString(@"CARD", nil);
self.imgCashCard.image = [UIImage imageNamed:@"card"];
}
else if (payment==1)
{
self.lblPayment.text = NSLocalizedString(@"CASH", nil);
self.imgCashCard.image = [UIImage imageNamed:@"cash"];
}
}

Kiran,你可以在获得boolValue之前检查payment_type是否为空。

NSUserDefaults *pref=[NSUserDefaults standardUserDefaults];
strUserId = [pref valueForKey:PARAM_ID];
strUserToken = [pref valueForKey:PARAM_TOKEN];
NSMutableDictionary *dictparam=[[NSMutableDictionary alloc]init];
[dictparam setObject:strUserId forKey:PARAM_ID];
[dictparam setObject:strUserToken forKey:PARAM_TOKEN];
[dictparam setObject:struser_longi forKey:PARAM_LONGITUDE];
[dictparam setObject:struser_lati forKey:PARAM_LATITUDE];
[dictparam setObject:strRequsetId forKey:PARAM_REQUEST_ID];
[dictparam setObject:@"0" forKey:PARAM_DISTANCE];
AFNHelper *afn=[[AFNHelper alloc]initWithRequestMethod:POST_METHOD];
[afn getDataFromPath:FILE_WALK_LOCATION withParamData:dictparam withBlock:^(id response, NSError *error)
{
NSLog(@"Update Walk Location = %@",response);
if (response)
{
if([response valueForKey:@"payment_type"] != nil && ![[response valueForKey:@"payment_type"] isEqualToString: ""])
{
payment=[[response valueForKey:@"payment_type"] boolValue];
NSLog(@"Something To Print");
NSLog(@"%d", payment);
NSLog(@"Something To Print");
if (payment==0)
{
self.lblPayment.text = NSLocalizedString(@"CARD", nil);
self.imgCashCard.image = [UIImage imageNamed:@"card"];
}
else if (payment==1)
{
self.lblPayment.text = NSLocalizedString(@"CASH", nil);
self.imgCashCard.image = [UIImage imageNamed:@"cash"];
}
if([[response valueForKey:@"success"] intValue]==1)
{
totalDist=[[response valueForKey:@"distance"]floatValue];
[self.btnDistance setTitle:[NSString stringWithFormat:@"%.2f %@",[[response valueForKey:@"distance"] floatValue],[response valueForKey:@"unit"]] forState:UIControlStateNormal];
}
else
{
[self.btnDistance setTitle:[NSString stringWithFormat:@"%.2f %@",[[response valueForKey:@"distance"] floatValue],[response valueForKey:@"unit"]] forState:UIControlStateNormal];
if ([[response valueForKey:@"is_cancelled"] integerValue]==1)
{
[self.timerForCancelRequest invalidate];
[self.timerForDestinationAddr invalidate];
NSUserDefaults *pref=[NSUserDefaults standardUserDefaults];
[pref removeObjectForKey:PREF_REQUEST_ID];
[pref removeObjectForKey:PREF_NAV];
strRequsetId=[pref valueForKey:PREF_REQUEST_ID];

[APPDELEGATE showToastMessage:NSLocalizedString(@"Request Canceled", nil) ];

is_walker_started=0;
is_walker_arrived=0;
is_started=0;
[btnWalker setHidden:NO];
[btnArrived setHidden:YES];
[btnWalk setHidden:YES];
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
}
}
}];

希望这就是你想要的。

最新更新