[英]getting “*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber compare:]: nil argument'”
Though we have lot of people asked same question , none of the answers are helping to fix my issue. 尽管我们有很多人问同样的问题,但是没有一个答案可以解决我的问题。 So here is my code , 这是我的代码,
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *text = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Response: %@", text);
//The NSJSONSerialization method to transform the NSData responseObject into a dictionnary does work
NSJSONSerialization *jsonResponse = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
//This NSLog makes the app crash with an unrecognized selector sent error
NSLog(@"JSON: %@",jsonResponse);
NSArray *result1=(NSArray *)[jsonResponse valueForKeyPath:@"result"];
NSLog(@"result1: %@",result1);
NSMutableString *shipPrice =[[NSMutableString alloc]init];
NSMutableString *freeLimitString =[[NSMutableString alloc]init];
if(result1!=Nil && ![result1 count]==0)
{
for (int i=0; i<result1.count; i++)
{
NSNumber *totalShippingPrice = [[result1 objectAtIndex:i] valueForKeyPath:@"totalShippingPrice"];
if( totalShippingPrice != nil && totalShippingPrice && [totalShippingPrice isEqualToNumber:0]&& [totalShippingPrice intValue]==0) // ISSUE LINE
{
shipPrice = (NSMutableString*)@"FREE";
} } } }
I kept breakpoint and exactly its happening in this above line. 我保留了断点及其确切发生在上面这一行的内容。 Im new to AFNetworking
. 我是AFNetworking
。 Im not sure how to fix it. 我不确定如何修复它。 Help me with working code sample :) Thanks 帮助我提供工作代码示例:)谢谢
You need to use this: 您需要使用此:
[totalShippingPrice isEqualToNumber: [NSNumber numberWithInt:0]]
In the line that crashes. 在崩溃的行中。 You need to compare the object with another one of the same type, if you use 0 is the same as if you were sending nil to it, so you need to create a NSNumber as I wrote. 您需要将该对象与另一个相同类型的对象进行比较,如果您使用0则与向其发送nil相同,因此您需要创建一个我编写的NSNumber。
Or just do: 或者只是做:
[totalShippingPrice intValue] == 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.