Resolved CGPoint conversion problem

Discussion in 'iPhone/iPad Programming' started by chen8002004, May 26, 2011.

  1. chen8002004, May 26, 2011
    Last edited: May 27, 2011

    macrumors newbie

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

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #2
    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:

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

Share This Page