在iphone上将nil值指定为double

iOS*_*Dev 2 iphone objective-c

我有这个方法=>

-(id)initWithLocationName:(NSString *)locatNm latitude:(double)lati longitude:(double)longi xCoord:(double)xco yCoord:(double)yco;
Run Code Online (Sandbox Code Playgroud)

在这种方法中,我需要为xCoord和yCoord分配nil值.如何将nil指定为double?

小智 12

nil用于Objective-C对象,并且double是原始数据类型(即,它不是Objective-C对象),因此您无法分配nildouble变量.

如果0.0不是表示缺少价值的选项,则可以使用NANisnan不是.例如:

#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
Run Code Online (Sandbox Code Playgroud)