views:

61

answers:

2
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?

A: 

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.

BobbyShaftoe
But what should I use for primitive types like float?
G.P. Burdell
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.
BobbyShaftoe
A: 

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

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

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

Mark
Well, specifically id types not pointers to C types.
BobbyShaftoe