直接设置属性和使用其setter之间的区别? [英] Difference between setting a property directly and using its setter?
本文介绍了直接设置属性和使用其setter之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这两者有什么区别?
-
self.view.color = [UIColor redColor];
-
[self.view setColor:[UIColor redColor]];
self.view.color = [UIColor redColor];
[self.view setColor:[UIColor redColor]];
推荐答案
没事.
- 使用
self.view.color = ...
隐式调用属性的setter. - 使用
[self.view setColor:...]
可以显式调用设置器.
- With
self.view.color = ...
the property's setter in called implicitly. - With
[self.view setColor:...]
you call the setter explicitly.
更多详细信息:UIView
具有类似以下内容的color
属性:
More in detail: UIView
has a color
property something like:
@property (nonatomic, strong) UIColor* color;
在Objective-C中,这会自动定义基础ivar _color
,并为setter和getter生成代码. (生成的代码取决于修饰符nonatomic
,...您指定.)
In Objective-C this automatically defines an underlying ivar _color
, and generates code for the setter and getter. (What code is generated depends on the modifiers nonatomic
, ... you specify.)
当您键入self.view.color = ...
时,编译器会看到您的color
是属性,并在内部将其替换为[self.view setColor:...]
.
When you type self.view.color = ...
the compiler sees that your color
is a property, and internally replaces it with [self.view setColor:...]
.
这篇关于直接设置属性和使用其setter之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文