4
if (myFloat == nil){
    \\Do Method
}

In the above situation, the method will run only if myFloat is nil. Will it also run if myFloat was set to 0?

3
  • Is myFloat an actual float type? If so, you shouldn't ever be setting/comparing it to nil, which is only valid for object references. It's an apples-to-oranges comparison, even though in this specific case it might work, as nil and zero are essentially the same thing when the code is compiled.
    – Ben Zotto
    Jun 17, 2010 at 4:08
  • Is there a nil equivalent for types like float? Jun 17, 2010 at 4:19
  • @G P Burdell: No, there isn't because float is not a pointer type. Well, actually, I guess you could use NaN for floating point values. steve.hollasch.net/cgindex/coding/ieeefloat.html
    – JeremyP
    Jun 17, 2010 at 8:10

3 Answers 3

1

nil should only be used with pointers. It says that the pointer has not been set to a value.

Floats and other C types just have a value. (Strictly floats and double possibly can have values like NaN but this is more difficult to manage)

In Objective C you can wrap a float in the class NSNumber. An object of this class is referenced by a pointer so a variable of type NSNumber* can be nil.

1
0

Well, nil is technically 0. However, some of this depends on what type of variable myFloat is. If myFloat is a C float, you can't depend on it being exactly 0. You really should be using nil on id types.

2
  • But what should I use for primitive types like float? Jun 17, 2010 at 4:13
  • It depends on what you are doing. If you have a pointer to a float, you can compare to 0. However, you should look at how floating point numbers are represented in a computer. Jun 17, 2010 at 4:31
0

Before I get into my answer, see this table from NSHipster for a refresher on the meanings of the family of null-like symbols:

Symbol Value Meaning
NULL (void *)0 literal null value for C pointers
nil (id)0 literal null value for Obj-C objects
Nil (Class)0 literal null value for Obj-C classes
NSNull [NSNull null] singleton object used to represent null

As the value column above shows, all but NSNull have 0 as their underlying value.

I ran a few test cases related to nil-checking and zero-checking.

Key:

  • ✅ condition was satisfied, so the log printed
  • ❌ condition was not satisfied, so the log didn't print
NSNumber* nilNSNumber = nil;
if(nilNSNumber == nil){
    NSLog(@"nilNSNumber == nil"); // ✅
}
if(nilNSNumber == 0){
    NSLog(@"nilNSNumber == 0"); // ✅
}

NSNumber* zeroNSNumberA = 0;
if(zeroNSNumberA == nil){
    NSLog(@"zeroNSNumberA == nil"); // ✅
}

NSNumber* zeroNSNumberB = [[NSNumber alloc] initWithInt:0];
if(zeroNSNumberB == nil){
    NSLog(@"zeroNSNumberB == nil"); // ❌
}
if(zeroNSNumberB == 0){
    NSLog(@"zeroNSNumberB == 0"); // ❌
}

NSObject* objectUninitialised = [NSObject alloc];
if(objectUninitialised == nil){
    NSLog(@"objectUninitialised == nil"); // ❌
}
if(objectUninitialised == 0){
    NSLog(@"objectUninitialised == 0"); // ❌
}

NSObject* objectInitialised = [objectUninitialised init];
if(objectInitialised == nil){
    NSLog(@"objectInitialised == nil"); // ❌
}
if(objectUninitialised == 0){
    NSLog(@"objectInitialised == 0"); // ❌
}

if([NSNull null] == 0){
    NSLog(@"NSNull == 0"); // ❌
}
if([NSNull null] == nil){
    NSLog(@"NSNull == nil"); // ❌
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.