目标 C - 如果没有什么不同,则使用访问器 [英] Objective C - Using an accessor if It does nothing different
问题描述
在目标c中,如果使用getter和直接访问ivar做完全一样的事情,getter中没有延迟加载代码,它所做的只是返回ivar,你仍然使用访问器还是直接访问ivar既然没有区别?为什么?
In objective c, if the using the getter and directly accessing the ivar do exactly the same thing, no lazy loading code in the getter, all it does is returns the ivar, would you still use the accessor or access the ivar directly since there is no difference? Why?
我说的是课堂内.
推荐答案
我决定始终使用 [self ivar],而不是直接使用 ivar,即使我使用标准的 ObjC 括号表示法,而不是点表示法.唯一的例外是,如果 [self ivar] 是一个延迟加载的访问器并且我已经在方法中使用了它并且我知道它已经被初始化并且我不想检查它是否为 nil 我在方法.
I decided to always use the [self ivar], not directly ivar, even though I use standard ObjC bracket notation , not dot notation. Only exception is if [self ivar] is a lazy-loading accessor and I already used it in the method and I know it has been initialised and I don't want to check if it is nil the 10 more times I use it in the method.
这篇关于目标 C - 如果没有什么不同,则使用访问器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!