自变量和变量差 [英] self.variable and variable difference
问题描述
self.myVariable = obj;
和 myVariable = obj;
之间有什么区别, code> @propery / @synthesize
创建`myVariable?
What is the difference between self.myVariable = obj;
and myVariable = obj;
, when I use @propery
/@synthesize
to create `myVariable?
推荐答案
重要的是注意点语法被编译器转换为一个简单的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屋!