views:

3580

answers:

4

Hi -

I'm getting a strange error when assigning a value to one of my objects. As best as I can figure out, the compiler thinks I'm assigning to a read only variable. It gives me "lvalue required as left operand of assignment" on the myPlace.publicLocation... line.

I'm sure it's a classic novice mistake - where am I going wrong?

CSPlace *myPlace;
myPlace.publicLocation.coordinate.latitude = 
    [[separatedData objectAtIndex:0] floatValue];

class:

#import <CoreLocation/CLLocation.h>
@interface CSPlace : NSObject {
    CLLocation *publicLocation;
}
@property (copy, nonatomic) CLLocation *publicLocation;
@end
#import "CSPlace.h"
@implementation CSPlace
@synthesize publicLocation;
@end
A: 

I'm no Objective-C expert, but I understand it's a C-superset... so shouldn't it be:

myPlace->publicLocation->coordinate.latitude

(with "->" instead of ".", I don't know for "coordinate", if it's a pointer or not )

Edit: Also, your program is very likely to crash if you try to access the attributes of a non-allocated object, myPlace in your case is not allocated.

Edit 2: see johne's comment below for why my answer should be ignored. (I'm leaving it though as it might serve someone someday making the same mistake)

Florian
ObjC2 introduced a new 'feature' called properties. In what must rank as one of the dumbest language design choices ever, they chose to overload the standard C '.' operator for this purpose. Not only does this comically break in strange and mysterious ways that are hard to figure out, properties can be both l and r values. Add to this the fact that properties can be read-only or read-write, which means deciphering a chain of property accesses for an lvalue can be ambiguous.
johne
Thank you johne for the explanation :) weird choice indeed, very off-putting.
Florian
A: 

coordinate is defined as a read-only property of CLLocation... so it appears you need to make a new CLLocation rather than changing the coordinate value of an existing one.

David Maymudes
A: 

As best as I can figure out, the compiler thinks I'm assigning to a read only variable.

You are attempting to assign to a read only property:

@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;

Jim Correia
+3  A: 

CLLocation an immutable class with respect to latitude and longitude. So you cannot modify its latitude. You have to crate a new object:

[[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

Being immutable is the whole meaning of having such an initializer.

IlDan
bonus points for getting a new concept into my thick skull :)
BankStrong