I'm doing a check in an iPhone application -
我正在检查iPhone应用程序 -
int var;
if (var != nil)
It works, but in X-Code this is generating a warning "comparison between pointer and integer." How do I fix it?
它可以工作,但在X-Code中,这会产生一个警告“指针和整数之间的比较”。我如何解决它?
I come from the Java world, where I'm pretty sure the above statement would fail on compliation.
我来自Java世界,在那里我很确定上面的陈述会因为赞美而失败。
2 个解决方案
#1
36
Primitives can't be nil
. nil
is reserved for pointers to Objective-C objects. nil
is technically a pointer type, and mixing pointers and integers will without a cast will almost always result in a compiler warning, with one exception: it's perfectly ok to implicitly convert the integer 0 to a pointer without a cast.
基元不能为零。 nil保留用于指向Objective-C对象的指针。 nil在技术上是指针类型,混合指针和整数将没有强制转换将几乎总是导致编译器警告,但有一个例外:完全可以将整数0隐式转换为没有强制转换的指针。
If you want to distinguish between 0 and "no value", use the NSNumber
class:
如果要区分0和“无值”,请使用NSNumber类:
NSNumber *num = [NSNumber numberWithInt:0];
if(num == nil) // compare against nil
; // do one thing
else if([num intValue] == 0) // compare against 0
; // do another thing
#2
10
if (var) {
...
}
Welcome to the wonderful world of C. Any value not equal to the integer 0 or a null pointer is true.
欢迎来到C的精彩世界。任何不等于整数0或空指针的值都是真的。
But you have a bug: ints cannot be null. They're value types just like in Java.
但你有一个bug:int不能为null。它们就像在Java中一样是值类型。
If you want to "box" the integer, then you need to ask it for its address:
如果你想“整理”整数,那么你需要询问它的地址:
int can_never_be_null = 42; // int in Java
int *can_be_null = &can_never_be_null; // Integer in Java
*can_be_null = 0; // Integer.set or whatever
can_be_null = 0; // This is setting "the box" to null,
// NOT setting the integer value
#1
36
Primitives can't be nil
. nil
is reserved for pointers to Objective-C objects. nil
is technically a pointer type, and mixing pointers and integers will without a cast will almost always result in a compiler warning, with one exception: it's perfectly ok to implicitly convert the integer 0 to a pointer without a cast.
基元不能为零。 nil保留用于指向Objective-C对象的指针。 nil在技术上是指针类型,混合指针和整数将没有强制转换将几乎总是导致编译器警告,但有一个例外:完全可以将整数0隐式转换为没有强制转换的指针。
If you want to distinguish between 0 and "no value", use the NSNumber
class:
如果要区分0和“无值”,请使用NSNumber类:
NSNumber *num = [NSNumber numberWithInt:0];
if(num == nil) // compare against nil
; // do one thing
else if([num intValue] == 0) // compare against 0
; // do another thing
#2
10
if (var) {
...
}
Welcome to the wonderful world of C. Any value not equal to the integer 0 or a null pointer is true.
欢迎来到C的精彩世界。任何不等于整数0或空指针的值都是真的。
But you have a bug: ints cannot be null. They're value types just like in Java.
但你有一个bug:int不能为null。它们就像在Java中一样是值类型。
If you want to "box" the integer, then you need to ask it for its address:
如果你想“整理”整数,那么你需要询问它的地址:
int can_never_be_null = 42; // int in Java
int *can_be_null = &can_never_be_null; // Integer in Java
*can_be_null = 0; // Integer.set or whatever
can_be_null = 0; // This is setting "the box" to null,
// NOT setting the integer value