开发者

Assign nil value to double in Objective-C

In my iOS iPhone app I have this method:

-(id)initWithLocationName:(NSString *)locatNm lat开发者_开发知识库itude:(double)lati longitude:(double)longi xCoord:(double)xco yCoord:(double)yco;

In this method I need to assign nil value to xCoord & yCoord.

How can I assign nil to double?


nil is for Objective-C objects, and double is a primitive data type (i.e., it isn’t an Objective-C object), hence you cannot assign nil to a double variable.

If 0.0 is not an option to represent the absence of value, you can use NAN and isnan instead. For instance:

#include <math.h>

double x = 0.0;
double y = NAN;

if (isnan(x)) NSLog(@"x is not a number"); // this won't be logged
if (isnan(y)) NSLog(@"y is not a number"); // this will be logged


You cannot have a double value of nil. It's not a reference type, it's a primitive. What exactly are you trying to accomplish?

Aside:

If you weren't trying to use it in this method, you could utilize [NSNumber numberWithFloat:0.4f] and as any pointer, NSNumber *number = nil; is perfectly valid. But it won't work in this scenario, since the method requires double.


Nil is just 0. Setting them to 0 will do the same thing that setting them to nil would do.


You can only assign nil to pointer types. You can assign 0.0 to a double however.


You could use NSNumber.


You can only assign nil to pointers. Instead of using nil, I would use a special value, usually -1. If you really need to use nil, you'll need to wrap the double in a NSNumber.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜