This question is not a “how do I” but rather a “why is this” concerning some Class properties in Objective-C.
I understand that the two examples below are not allowed and result in an error (“expression not assignable”):
self.myUIView.frame.origin.x = 50;
self.myUIView.frame.origin = CGPointMake(50,50);
and that the proper way to do this is to create a new CGRect, set the values desired and then assign it:
CGRect rect = self.myUIView.frame;
rect.origin = CGPointMake(50, 50);
self.myUIView.frame = rect;
I’d like to understand why this is.
CGRect is a struct containing two structs. Does the restriction have to do with how structs are allocated, to prevent an assignment which might overflow that memory? Or is it more of a convention concerning public and private properties?
I realize this seems like a minor/basic question but I think it is important to clear up muddy/bad understandings of these fundamental sort of things.
It’s because if you were able to directly change a frame’s origin or size, you would bypass the setter method of UIView’s frame property. This would be bad for multiple reasons.
UIView would have no chance to be notified about changes to it’s frame. But it has to know about these changes to be able to update it’s subviews or redraw itself.
Also, for KVO to work, you have to use the correct setters (either by using setFrame: or the dot notation).