获取“***因未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:' - [__ NSCFNumber compare:]:nil参数'”

时间:2013-07-08 14:03:31

标签: ios objective-c afnetworking

虽然我们有很多人问同样的问题,但没有一个答案有助于解决我的问题。所以这是我的代码,

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

我保留了断点,正好在上面这行中发生了。我是AFNetworking的新手。我不知道如何解决它。帮我处理代码示例:)谢谢

1 个答案:

答案 0 :(得分:6)

你需要使用它:

[totalShippingPrice isEqualToNumber: [NSNumber numberWithInt:0]]

在崩溃的线路中。 你需要将对象与另一个相同类型的对象进行比较,如果你使用0就像你发送nil一样,所以你需要在我写的时候创建一个NSNumber。

或者只是这样做:

[totalShippingPrice intValue] == 0