Compare a NSNumber to an int

后端 未结 5 2176
长发绾君心
长发绾君心 2021-01-01 17:39

I\'ve a simple question (I think): I\'m trying to compare a NSNumber with a int, to see if it is 0 or 1. Here is the code:

id i = [dictionary objectForKey:@\         


        
相关标签:
5条回答
  • 2021-01-01 18:05
    1. The result of comparison is a BOOL which is not an Objective-C object. Therefore you should not print it using %@. Try %d instead (shows 0 or 1).

    2. [a compare:b] returns -1 if a < b, 0 if a == b and 1 if a > b. So your 2nd result is expected.

    3. You cannot compare an NSNumber directly with an integer. That i == 0 is actually a pointer comparison which checks whether i is NULL (0), which of course is FALSE if that number exists. So the 1st result is also expected.

    4. If you want to check for equality, use [a isEqualToNumber:b]. Alternatively, you could extract the integer out with [a intValue] and compare with another integer directly.

    So the followings should work:

    NSLog(@"%@ == 0 -> %d", i, [i isEqualToNumber:[NSNumber numberWithInt:0]]);
    NSLog(@"%@ == 0 -> %d", i, [i intValue] == 0);
    

    If the "number" is in fact a boolean, it's better to take the -boolValue instead.

    NSLog(@"%@ == 0 -> %d", i, ! [i boolValue]);
    
    0 讨论(0)
  • 2021-01-01 18:06

    You can easily write:

    NSNumber *number = [NSNumber numberWithInt:123];
    int integer = 1234;
    
    NSLog(@"%@ == %i : %i", number, integer, [number intValue] == integer);
    

    Output should be

    123 == 1234 : 0
    


    I hope i can help you!

    0 讨论(0)
  • 2021-01-01 18:08

    Here you're comparing the pointer of the object i with 0, which I'm afraid is not what you want.

    You most probably want to compare the value of i:

    if ([i intValue]==0) {
      ...
    }
    
    0 讨论(0)
  • 2021-01-01 18:09

    This worked

    if (thevariable.intValue==0) {
      ...
    }
    
    0 讨论(0)
  • 2021-01-01 18:24

    You have two problems:

    • You are confusing the NSNumber object with the value it represents.
    • Your NSLog format string does not match the types of the arguments that you provide.

    Regarding the first problem: i is an address, perhaps something like 0x1f84b. When you test whether i == 0, you are testing whether i == NULL. In this case, that means you are testing whether the key "error" was present in the dictionary or not, since looking up a non-existent key garners a NULL.

    [i intValue], on the other hand, is an integer. If the NSNumber contains a value representable as an integer, this will be the value of the NSNumber. That is what you see when you print the NSNumber's description using the %@ format specifier.

    Regarding the second problem: Comparisons in C and Objective-C return an integer, either 0 (meaning false) or 1 (meaning true). In order to directly print the result of a comparison, you thus need to use the integer format specifier. There are actually two such specifiers, %i and %d. You could wrap the result of the comparison in an NSNumber and use %@ to print that, but that's more work than it's worth.

    So, here's what you should be doing:

    NSNumber *i = [dictionary objectForKey:@"error"];
    BOOL haveValue = (i != NULL);
    if (haveValue) {
        int iValue = [i intValue];
        NSLog(@"%d == 0 -> %d", iValue, iValue == 0);
        NSLog(@"%@ compared to 0 -> %d", i, [i compare:[NSNumber numberWithInt:0]]);
    } else {
        NSLog(@"*** Dictionary has no value for key \"error\"!");
    }
    
    0 讨论(0)
提交回复
热议问题