ios – 表达式不可分配,MKAnnotation类委托的坐标属性

前端之家收集整理的这篇文章主要介绍了ios – 表达式不可分配,MKAnnotation类委托的坐标属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我做了这个架构,以便更好地解释我的麻烦是什么.

那么,我该怎么做才能解决它呢?谢谢=)

解决方法

CLLocationCoordinate2D是结构,即值类型.它是通过值传递的,这是另一种说“复制”的方式.如果您分配其字段(例如经度),那么所有操作都是修改副本;注释中的原始坐标将保持不变.这就是该财产不可转让的原因.

解决此问题,您应该为纬度和经度添加单独的属性,然后使用它们:

@interface Annotation : NSObject<MKAnnotation>
    @property (readwrite) CLLocationDegrees latitude;
    @property (readwrite) CLLocationDegrees longitude;
    @property (nonatomic,assign) CLLocationCoordinate2D coordinate;
    ...
@end

@implementation Annotation
    -(CLLocationDegrees) latitude {
        return _coordinate.latitude;
    }
    -(void)setLatitude:(CLLocationDegrees)val {
        _coordinate.latitude = val;
    }
    -(CLLocationDegrees) longitude{
        return _coordinate.longitude;
    }
    -(void)setLongitude:(CLLocationDegrees)val {
        _coordinate.longitude = val;
    }
@end

现在您的XML解析器代码可以执行此操作:

if ([llave isEqualTo:@"lat"]) {
    puntoXML.latitude = [valor doubleValue];
} else if ([llave isEqualTo:@"lon"]) {
    puntoXML.longitude = [valor doubleValue];
} ...

猜你在找的Xcode相关文章