自变量和变量差异 [英] self.variable and variable difference
问题描述
self.myVariable = obj;
和 myVariable = obj;
有什么区别,当我使用 @propery
/@synthesize
创建`myVariable?
What is the difference between self.myVariable = obj;
and myVariable = obj;
, when I use @propery
/@synthesize
to create `myVariable?
推荐答案
需要注意的是,编译器将 dot-syntax 转换为一个简单的 objc_msgSend 调用:也就是说,它的作用与发送到的消息完全一样该变量的访问器.因此,以下所有三个都是等效的:
It's important to note that dot-syntax is converted to a simple objc_msgSend call by the compiler: that is to say that underneath it acts exactly like a message send to the accessor for that variable. As such, all three of the following are equivalent:
self.myVariable = obj;
[self setMyVariable:obj];
objc_msgSend(self, @selector(setMyVariable:), obj);
当然,这意味着使用点语法实际上会导致完整的消息发送,这意味着调用一个新函数以及与之相关的所有开销.相比之下,使用简单赋值 (myVariable = obj;) 不会产生任何此类开销,但当然只能在相关类的实例方法中使用.
Of course, this means that using dot-syntax actually results in a full message send, meaning calling a new function and all the overhead that is associated with it. In contrast, using simple assignment (myVariable = obj;) incurs none of this overhead, but of course it can only be used within the instance methods of the class in question.
这篇关于自变量和变量差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!