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:
我有一个简单的问题(我想):我试图比较NSNumber和int,看看它是0还是1。这是代码:
id i = [dictionary objectForKey:@"error"]; //class = NSCFNumber
NSLog(@"%@ == 0 -> %@", i, i == 0);
NSLog(@"%@ == 0 -> %@", i, [i compare:[NSNumber numberWithBool:NO]]);
I tried this to methods but I get null as result:
我尝试了这个方法,但结果是空的:
2010-10-17 21:57:49.065 Api[15152:a0f] 0 == 0 -> (null)
2010-10-17 21:57:49.065 Api[15152:a0f] 0 == 0 -> (null)
Can you help me?
你能帮我吗?
5 个解决方案
#1
43
-
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).比较的结果是一个BOOL,它不是Objective-C对象。因此,不应该使用%@打印它。尝试%d(显示0或1)。
-
[a compare:b]
returns -1 ifa < b
, 0 ifa == b
and 1 ifa > b
. So your 2nd result is expected.[a compare:b]如果a < b,返回-1;如果a = b,返回0;如果a = b,返回1。
-
You cannot compare an NSNumber directly with an integer. That
i == 0
is actually a pointer comparison which checks whetheri
is NULL (0), which of course is FALSE if that number exists. So the 1st result is also expected.不能直接将NSNumber与整数进行比较。i == 0实际上是一个指针比较,它检查i是否为NULL(0),如果这个数字存在,它当然是假的。所以第一个结果也是预期的。
-
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.如果你想检查平等,使用[a isEqualToNumber:b]。或者,您可以使用[一个intValue]提取整数,并直接与另一个整数进行比较。
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.
如果“数字”实际上是一个布尔值,最好使用-boolValue代替。
NSLog(@"%@ == 0 -> %d", i, ! [i boolValue]);
#2
9
Here you're comparing the pointer of the object i with 0, which I'm afraid is not what you want.
这里你比较的是对象i和0的指针,恐怕不是你想要的。
You most probably want to compare the value of i:
你可能想要比较i的值:
if ([i intValue]==0) {
...
}
#3
3
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!
我希望我能帮助你!
#4
3
You have two problems:
你有两个问题:
- You are confusing the NSNumber object with the value it represents.
- 您将NSNumber对象与它表示的值混淆。
- Your
NSLog
format string does not match the types of the arguments that you provide. - 您的NSLog格式字符串不匹配您提供的参数的类型。
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是一个地址,比如0x1f84b。当您测试i是否= 0时,您是在测试i是否== NULL。在本例中,这意味着您正在测试是否在字典中出现了关键的“错误”,因为查找不存在的密钥garners为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.
另一方面,[i intValue]是一个整数。如果NSNumber包含一个可表示为整数的值,那么这个值就是NSNumber的值。当您使用%@格式说明符打印NSNumber的描述时,您将看到这一点。
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.
关于第二个问题:C和Objective-C中的比较返回一个整数,0(表示false)或1(表示true)。为了直接打印比较的结果,因此需要使用整数格式说明符。实际上有两个这样的说明符,%i和%d。您可以将比较的结果封装在一个NSNumber中,并使用%@来打印它,但这比它的价值要大得多。
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\"!");
}
#5
0
This worked
这个工作
if (thevariable.intValue==0) {
...
}
#1
43
-
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).比较的结果是一个BOOL,它不是Objective-C对象。因此,不应该使用%@打印它。尝试%d(显示0或1)。
-
[a compare:b]
returns -1 ifa < b
, 0 ifa == b
and 1 ifa > b
. So your 2nd result is expected.[a compare:b]如果a < b,返回-1;如果a = b,返回0;如果a = b,返回1。
-
You cannot compare an NSNumber directly with an integer. That
i == 0
is actually a pointer comparison which checks whetheri
is NULL (0), which of course is FALSE if that number exists. So the 1st result is also expected.不能直接将NSNumber与整数进行比较。i == 0实际上是一个指针比较,它检查i是否为NULL(0),如果这个数字存在,它当然是假的。所以第一个结果也是预期的。
-
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.如果你想检查平等,使用[a isEqualToNumber:b]。或者,您可以使用[一个intValue]提取整数,并直接与另一个整数进行比较。
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.
如果“数字”实际上是一个布尔值,最好使用-boolValue代替。
NSLog(@"%@ == 0 -> %d", i, ! [i boolValue]);
#2
9
Here you're comparing the pointer of the object i with 0, which I'm afraid is not what you want.
这里你比较的是对象i和0的指针,恐怕不是你想要的。
You most probably want to compare the value of i:
你可能想要比较i的值:
if ([i intValue]==0) {
...
}
#3
3
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!
我希望我能帮助你!
#4
3
You have two problems:
你有两个问题:
- You are confusing the NSNumber object with the value it represents.
- 您将NSNumber对象与它表示的值混淆。
- Your
NSLog
format string does not match the types of the arguments that you provide. - 您的NSLog格式字符串不匹配您提供的参数的类型。
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是一个地址,比如0x1f84b。当您测试i是否= 0时,您是在测试i是否== NULL。在本例中,这意味着您正在测试是否在字典中出现了关键的“错误”,因为查找不存在的密钥garners为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.
另一方面,[i intValue]是一个整数。如果NSNumber包含一个可表示为整数的值,那么这个值就是NSNumber的值。当您使用%@格式说明符打印NSNumber的描述时,您将看到这一点。
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.
关于第二个问题:C和Objective-C中的比较返回一个整数,0(表示false)或1(表示true)。为了直接打印比较的结果,因此需要使用整数格式说明符。实际上有两个这样的说明符,%i和%d。您可以将比较的结果封装在一个NSNumber中,并使用%@来打印它,但这比它的价值要大得多。
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\"!");
}
#5
0
This worked
这个工作
if (thevariable.intValue==0) {
...
}