Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 处理Objective-C中的空结构(自定义类中的坐标)_Iphone_Objective C_Struct - Fatal编程技术网

Iphone 处理Objective-C中的空结构(自定义类中的坐标)

Iphone 处理Objective-C中的空结构(自定义类中的坐标),iphone,objective-c,struct,Iphone,Objective C,Struct,我有一个自定义类,作为坐标的实例变量: CLLocationCoordinate2D eventLocation; @property(nonatomic) CLLocationCoordinate2D eventLocation; 我正在解析一个xml文件,该文件有一个可选字段,该字段可能存在,也可能不存在。 如果是,我将其设置为: CLLocationCoordinate2D location; NSArray *coordinateArray = [paramValue componen

我有一个自定义类,作为坐标的实例变量:

CLLocationCoordinate2D eventLocation;
@property(nonatomic) CLLocationCoordinate2D eventLocation;
我正在解析一个xml文件,该文件有一个可选字段,该字段可能存在,也可能不存在。 如果是,我将其设置为:

CLLocationCoordinate2D location;
NSArray *coordinateArray = [paramValue componentsSeparatedByString:@","];
if ([coordinateArray count] >= 2) {
    location.latitude = [[coordinateArray objectAtIndex:0] doubleValue];
    location.longitude = [[coordinateArray objectAtIndex:1] doubleValue];
} else {
    NSLog(@"Coordinate problem");
}
info.eventLocation = location;
我所做的基本上是在地图上添加注释

annotation.coordinate = alert.info.eventLocation;
我知道我需要在这里做一些检查以确保它存在,但是我不允许做
if(info.eventLocation==nil)
或者甚至
if(info.eventLocation.latitude==nil)


这似乎是一个非常基本的问题,但我做了一些搜索,没有人能够真正提供一个好的答案/想法。我的架构完全关闭了吗?

因为
CLLocationCoordinate2D
是一个结构,所以没有
nil
值。如果结构是对象实例变量,Objective-C会将其初始化为0,因此如果未为
eventLocation
设置值,
annotation.coordinate.longitude
eventLocation.lattitude
都将为0。因为这是一个有效的位置,所以它不是一个有用的标记

我将定义一个非物理值:

static const CLLocationDegrees emptyLocation = -1000.0;
static const CLLocationCoordinate2D emptyLocationCoordinate = {emptyLocation, emptyLocation}

然后将此值指定给您的
alert.info.eventLocation=emptyLocationCoordination
,以表示空值。然后,您可以检查
(alert.info.eventLocation==emptyLocationCoordination)

我使用了上面的代码,但它不允许我用另一个常量声明一个常量,所以我只是将其更改为:

static const CLLocationDegrees EmptyLocation = -1000.0;
static const CLLocationCoordinate2D EmptyLocationCoordinate = {-1000.0, -1000.0};
我还在类的init中添加了:

eventLocation = EmptyLocationCoordinate;

感谢您的帮助,Barry。

为了完整起见,您可以使用
KCllocationCoordinated2Invalid
常量保存对无效
CLLocationCoordinated2D
的引用。(请参阅:)

当我将其添加到自定义对象的.h文件时,我遇到了错误:初始值设定项元素不是常量(“EmptyLocationCoordination.latitude”的初始值附近)初始值设定项元素不是常量(“EmptyLocationCoordination.longitude”的初始值附近),alert.info.eventLocation=EmptyLocationCoordination仍然会给出“二进制操作数无效!=”错误啊,也许你需要定义EmptyLocation。说Objective-C将结构初始化为0并不十分准确。它会将实例变量归零,但在堆栈上创建的结构与任何其他自动变量一样处理。您还可以使用
CLLocationCoordinate2DIsValid()
测试
CLLocationCoordinate2D
是否有效。如果它的纬度大于90度或小于-90度,或者它的经度大于180度或小于-180度,那么它将无效。