左值作为赋值的左操作数

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

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

框架是一种财产,而不是结构领域。 你不能分配给它的一个子域。 把它看作一个函数调用; 属性的点语法是方便的。

这个:

 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也有框架。

不要混合赋值左侧的属性访问器和结构字段访问。

左值是可以出现在赋值左侧的expression式。 当你混合使用属性时,结果expression式不是左值,所以你不能在赋值的左边使用它。

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

scrollButton.frame部分是属性访问。 .size部分访问frame结构的一个字段。 上面的Steven Fisher的例子是分解代码以避免问题的正确方法。

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

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

UIButton的框架属性是一个CGRect结构。 编译器会看到您的.size访问权限,并尝试将其parsing为不存在的setter。 而不是混合结构成员访问与属性访问器,你需要处理CGRect结构types作为一个整体…

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