可以说我有一个用强属性定义的customView。ARC中的自我分配或下划线分配?
@property (nonatomic, strong) UIView *customView;
什么是分配和初始化customView的最佳做法?
self.customView = [[UIView alloc] initWithFrame......
OR
_customView = [[UIView alloc] initWithFrame...
可以说我有一个用强属性定义的customView。ARC中的自我分配或下划线分配?
@property (nonatomic, strong) UIView *customView;
什么是分配和初始化customView的最佳做法?
self.customView = [[UIView alloc] initWithFrame......
OR
_customView = [[UIView alloc] initWithFrame...
这取决于你。如果你不想再次考虑它,你可以去“总是使用getters和setters”路线。或者,如果你不想浪费消息调用并用self.
(像我)抛弃你的代码,你可以去“使用实例变量,除非我需要需要来使用getters/setters”路由。
最好的办法是
self.customView = [[UIView alloc] initWithFrame......
因为如果我们使用这个变量与自为customView getter和setter只会被激活。否则,不需要声明该变量的属性。
方法::默认情况下,当您合成getter和setter访问器方法时,假定属性和ivar具有相同的名称。
对于何时使用getter/setter方法以及何时直接访问伊娃而言,这可能会让您第一眼感到困惑。
替代方法是命名伊娃不同于财产。常用的方法是使用下划线为ivars名称加上前缀。
你可以参考以下链接:Understanding Your (Objective-C) Self
感谢。
+1。 KVC/KVO也通过'self'发挥作用 – 2013-03-20 07:26:27