本文介绍了为什么“表达式不可分配” (UIView.frame.origin)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题不是我怎么做,而是关于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);

并且正确的方法是创建一个新的CGRect,设置所需的值然后分配它:

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是一个包含两个结构的结构。限制是否与分配结构有关,以防止可能溢出该内存的赋值?或者它更像是关于公共和私人财产的惯例?

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.

推荐答案

这是因为如果你能够直接改变一个框架的原点或大小,你会绕过UIView的frame属性的setter方法。这可能有多种原因。

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没有机会收到有关其框架更改的通知。但它必须知道这些更改才能更新它的子视图或重绘自己。

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.

此外,工作,你必须使用正确的setter(通过使用setFrame:或点表示法) )。

Also, for KVO to work, you have to use the correct setters (either by using setFrame: or the dot notation).

这篇关于为什么“表达式不可分配” (UIView.frame.origin)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:21