I have defined an object with a CGPoint ivar and associated property as follows…
@interface Sprite : NSObject {
CGRect boundingBox;
}
@property(assign) CGRect boundingBox;
When I use an instance of the Sprite class and try and update the boundingBox struct as follows…
self.boundingBox.origin.x = minX;
I receive a compile time error stating…
Lvalue required as left operand of
assignment
Am I correct in saying that self.boundingBox will return a C struct, which isn’t actually the struct held by the Sprite object but rather a copy of it? Therefore when the assignment of a new value for x fails because the struct is only a temporary copy?
If this is the case will the following code work correctly (and is it the correct way to achieve what I want)?
CGRect newBoundingBox = self.boundingBox;
newBoundingBox.origin.x = self.boundingBox.origin.x;
self.boundingBox = newBoundingBox;
EDITED ANSWER: My original comment about not being able to edit any CGRect was wrong; the actual problem with that first line of code comes from the fact that you’re using
self.boundingBoxto access theCGRect. Your second code will work great, along with the block I had in my original answer:To properly change values inside
boundingBox, you’ll need to use apple’sCGRectMakefunction, like so:For a nice explanation of why the
self.notation is a problem with structs, check out the answers to this question. Hope this helps!