Lvalue需要作为赋值的左操作数

时间:2011-04-03 06:00:13

标签: objective-c ios tags uiscrollview uibutton

我想将suitSize分配给scrollButton,我做错了什么?

UIView *scrollButton = [suitScrollView viewWithTag:1];
CGSize suitSize =CGSizeMake(10.0f,10.0f);
(UIButton *)scrollButton.frame.size=suitSize;

3 个答案:

答案 0 :(得分:5)

框架是属性,而不是结构字段。您无法分配给它的子字段。把它想象成一个函数调用;属性的点语法很方便。

此:

scrollButton.frame.size = suitSize;

相当于:

[scrollButton frame].size = suitSize;

哪个不起作用;分配给函数结果的字段没有任何意义。

相反,这样做:

CGFrame theFrame = [scrollButton frame];
theFrame.size = suitSize;
[scrollButton setFrame: theFrame];

或者,如果您愿意:

CGFrame theFrame = scrollButton.frame;
theFrame.size = suitSize;
scrollButton.frame = theFrame;

请注意,不必将scrollButton转换为UIButton; UIViews也有框架。

答案 1 :(得分:1)

不要在作业的左侧混合属性访问器和结构域访问。

左值是一个表达式,可以出现在作业的左侧。混合结构和属性时,结果表达式不是左值,因此不能在赋值的左侧使用它。

(UIButton *)scrollButton.frame.size=suitSize;

scrollButton.frame部分是属性访问权限。 .size部分访问frame结构的字段。史蒂芬费舍尔上面的例子是打破代码以避免问题的正确方法。

答案 2 :(得分:0)

在处理结构属性时,不能以这种方式直接设置子结构......

(UIButton *)scrollButton.frame.size=suitSize;

UIButton的frame属性是一个CGRect结构。编译器会看到您的.size访问权限并尝试将其解析为不存在的setter。您不需要将struct成员访问与属性访问器混合,而是需要整体处理CGRect结构类型...

CGRect frame = (UIButton *)scrollButton.frame;
frame.size = CGSizeMake(100, 100);
(UIButton *)scrollButton.frame = frame;