问题
I want to assign suitSize to scrollButton what I'm doing wrong?
UIView *scrollButton = [suitScrollView viewWithTag:1];
CGSize suitSize =CGSizeMake(10.0f,10.0f);
(UIButton *)scrollButton.frame.size=suitSize;
回答1:
frame is a property, not a structure field. You can't assign to a subfield of it. Think of it as a function call; dot syntax for properties is convenience.
This:
scrollButton.frame.size = suitSize;
Is equivalent to:
[scrollButton frame].size = suitSize;
Which doesn't work; it doesn't make any sense to assign to a field of a function result.
Instead, do this:
CGFrame theFrame = [scrollButton frame];
theFrame.size = suitSize;
[scrollButton setFrame: theFrame];
Or, if you prefer:
CGFrame theFrame = scrollButton.frame;
theFrame.size = suitSize;
scrollButton.frame = theFrame;
Note that casting the scrollButton to a UIButton isn't necessary; UIViews have frames, too.
回答2:
Don't mix the property accessors and struct field access on the left side of an assignment.
An lvalue is an expression that can appear on the left side of an assignment. When you mixstructs and properties, the resulting expression is not an lvalue, so you can't use it on the left side of an assignment.
(UIButton *)scrollButton.frame.size=suitSize;
The scrollButton.frame
part is a property access. The .size
part accesses a field of the frame
structure. Steven Fisher's example above is the right way to break up the code to avoid the problem.
回答3:
When dealing with properties that are structs you cannot directly set sub-structs in this manner...
(UIButton *)scrollButton.frame.size=suitSize;
The frame property of the UIButton is a CGRect struct. The compiler sees your .size access and tries to resolve it to a setter which does not exist. Instead of mixing struct member access with property accessors you need to deal with the CGRect struct type as a whole...
CGRect frame = (UIButton *)scrollButton.frame;
frame.size = CGSizeMake(100, 100);
(UIButton *)scrollButton.frame = frame;
来源:https://stackoverflow.com/questions/5527878/lvalue-required-as-left-operand-of-assignment