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

发布于 2024-10-29 19:43:19 字数 211 浏览 0 评论 0原文

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

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

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听闻余生 2024-11-05 19:43:20

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

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

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

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

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;
許願樹丅啲祈禱 2024-11-05 19:43:19

框架是一个属性,而不是结构字段。您不能分配给它的子字段。将其视为函数调用;属性的点语法很方便。

这:

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;只需将scrollButton 转换为UIButton 即可。 UIView 也有框架。

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.

不乱于心 2024-11-05 19:43:19

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

左值是可以出现在赋值左侧的表达式。当混合结构和属性时,生成的表达式不是左值,因此不能在赋值的左侧使用它。

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文