PDA

View Full Version : [Resolved] CGPoint conversion problem




chen8002004
May 26, 2011, 06:49 PM
I get “Lvalue required as left operand of assignment” error when compiling the following code. Can anyone help?
Declaration:
@interface myInfo: NSObject
{
CGPoint startPosition;
}
@property CGPoint startPosition;
@end
Definition:
@implementation myInfo
@synthesize startPosition;
@end
Use:
myInfo* temp = [[myInfo alloc] init];
int ttt=100;
temp.startPosition.x = (CGFloat)ttt; << Compile error here



Guiyon
May 26, 2011, 07:27 PM
The simple explanation is that the synthesized methods for structs (CGPoint, CGRect, etc) return a *copy* of the structure, not reference to it. Doing something like you are would simply set the value of 'x' on the returned copy and then discard the whole thing. You would need to do something like one of the below snippets:

CGPoint point = temp.startPosition;
point.x = 100;
temp.startPosition = point;

// Assumes that you want the 'y' value to remain constant
temp.startPosition = CGPointMake(100,temp.startPosition.y);