Expression is not assignable, coordinate attribute

2019-06-24 10:51发布

问题:

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.