Expression is not assignable, coordinate attribute from MKAnnotation class delegate

落爺英雄遲暮 提交于 2019-12-10 16:11:27

问题


I did this schema in order to explain better what is my troubles.

So, what can I do to fix it? Thank you =)


回答1:


The CLLocationCoordinate2D is a struct, i.e. a value type. It is passed around by value, which is another way of saying "copying". If you assign its fields (e.g. longitude) all that would do is modifying a copy; the original coordinate inside your Annotation would remain intact. That is why the property is not assignable.

To fix this, you should add separate properties for latitude and longitude, and use them instead:

@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

Now your XML parser code can do this:

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



回答2:


Change:

puntoXML.coordinate.latitude = [valor floatValue];

to:

CLLocationCoordinate2D coord = puntoXML.coordinate;
coord.latitude = [valor floatValue];
puntoXML.coordinate = coord;

Make a similar change for the longitude. Also note that you will need to add curly braces to the if statements.




回答3:


The issue is that you are assigning a copy of CLLocationCoordinate2D with your latitude/longitude.

puntoXML.coorinate returns a CLLocationCoordinate2D (a copy) so assigning latitude will have no effect.

Instead you need to create a complete CLLocationCoordinate2D with the new latitude and longitude and set that in one go.

EDIT better still provide separate properties for the latitude/longitude and provide a custom setter for each that sets their value in the coordinate instance variable.



来源:https://stackoverflow.com/questions/15161923/expression-is-not-assignable-coordinate-attribute-from-mkannotation-class-deleg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!