简体   繁体   English

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

[英]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.

相关问题 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“数据参数为nil” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ ***-[__ NSCFConstantString stringByAppendingString:]:无参数” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFConstantString stringByAppendingString:]: nil argument' 由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序,原因:&#39;UICollectionView必须使用非nil布局参数初始化 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter Swift:由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“实体名称不能为零。” - Swift : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Entity name must not be nil.' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“应用程序试图在目标上显示nil模态视图控制器 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ *** setObjectForKey:对象不能为nil(键:索引)” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: index)' 由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序,原因:&#39;+ entityForName:nil不是合法的NSManagedObjectContext - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext **由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序,原因:&#39;应用程序试图在目标上显示nil模态视图控制器 - ** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ *** setObjectForKey:键不能为零” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“-[SWRevealViewController manifestToggel:] - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SWRevealViewController revealToggel:]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM