自变量和变量的区别
当我使用 @propery
/@synthesize< 时,
self.myVariable = obj;
和 myVariable = obj;
有什么区别/code> 创建 `myVariable?
What is the difference between self.myVariable = obj;
and myVariable = obj;
, when I use @propery
/@synthesize
to create `myVariable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
值得注意的是,点语法被编译器转换为简单的 objc_msgSend 调用:也就是说,它的底层行为与发送到该变量的访问器的消息完全相同。 因此,以下所有三个都是等效的:
当然,这意味着使用点语法实际上会导致完整的消息发送,这意味着调用新函数以及与其相关的所有开销。 相反,使用简单赋值 (myVariable = obj;) 不会产生任何此类开销,但它当然只能在相关类的实例方法中使用。
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:
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.
@synthesize 指令告诉编译器根据 .h 文件中 @property 指令中给出的规范为成员变量生成访问器。 (即,如果您指定retain,则setter 将保留该变量,如果您指定copy,则它将复制该变量。)
访问器将(除非您另外指定)被命名为propertyName 和setPropertyName。
使用 。 符号(注意,不是上面所述的 self 语法)表示您想要使用访问器(例如,如果您正在设置字符串,并且想要确保保留计数正确,则这是一件好事)。
因此,在您的类实现中:
访问器 setBill.
直接,无需经过
访问器。
The @synthesize directive tells the compiler to generate accessors for your member variables, according to the specifications given in the @property directive in your .h file. (I.e., if you specify retain, the setter will retain the variable, and if you specify copy, it will copy it.)
The accessors will (unless you specify otherwise) be named propertyName and setPropertyName.
Using the . notation (note, not the self syntax as stated above) is saying that you want to use the accessors (a good thing if you are setting strings, and want to ensure the retain count is correct, for example).
So, within your class implementation:
accessor setBill.
directly, without going through the
accessor.
我在开始 Cocoa 开发时发现的差异之一是,如果我将变量设置为使用 @Property/@Synthesize 语法,并且我没有使用 self.myVariable = obj 或 [self setMyVariable :obj] 而是 myVariable = obj,如果稍后释放 obj,则不会保留该对象。 (假设 @Property 设置为使用保留。)
原因是使用 myVariable = obj 时未设置保留计数,并且当释放 obj 时,计数现在为零。 (除非您自己保留它)但是通过使用访问器,它将为您执行保留计数。 (再次假设您将其设置为在声明时使用保留)。
One of the differences I found out when starting Cocoa development is if I set variable to use a @Property/@Synthesize syntax and I didn't use self.myVariable = obj or [self setMyVariable:obj] but instead myVariable = obj, the object is not retained if obj is released later. (Assuming @Property was set up to use retain.)
The reason is the retain count is not set when using myVariable = obj and when the obj is released the count is now zero. (Unless you retain it yourself) But by using the accessor it will do the retain count for you. (Again assuming you set it up to use retain when it was declared).
Shyne
如果我可以对此添加一个重要说明。 上面的答案都很棒,所以我不会添加技术方面的内容。 但就是这样:
如果您创建一个合成属性,
请始终使用 self.myProp 模式来设置它。
这看起来很明显,但很重要。 确实,根本没有理由这样做,但在您真正理解合成设置器是如何创建之前,您只想假设您必须使用 self. 模式来设置值。
老实说:这将为您节省大量深夜调试时间。 非保留内存访问违规是最难调试的。
Shyne
If I can add one important note to this. The answer above are all awesome, so I won't add to the technical side. But just this:
If you create a synthesized property
Always use the self.myProp pattern to set it.
This seems really obvious, but it's important. It's true that there is simply no reason to do this, but until you really understand how the synthesized setters are created you just want to assume you HAVE to use the self. pattern to set the value.
Honest: this will save you a lot of late night debug sessions. Non-retained memory access violations are simply the worst to debug.
self
语法使用访问器方法,其他语法则不使用。 如果访问器执行的操作不仅仅是简单地分配新值,这可能会产生很大的差异。 请参阅 声明的属性是 Objective-C 教程的一部分。The
self
syntax uses the accessor method, the other syntax does not. This might be a big difference if the accessor does something more than simply assign the new value. See the Declared Properties part of the Objective-C tutorial.其他答案是正确的,不同之处在于点符号导致 ivar 通过附件而不是直接更改。
在您知道自己在做什么之前,我建议您使用点表示法(即
self.propertyName = ...
)。 Cocoa/Obj-C 在键值编码方面做了很多工作,虽然手机 SDK 没有充分利用这一点(通过绑定之类的东西),但最终它会的。 现在习惯使用访问器将为您在将来省去很多麻烦。使用访问器方法还使您有机会覆盖它们并在需要时提供更多功能。 通过简单地改变 ivar 的值,您就剥夺了自己的这种能力。
The other answers are correct, the difference is that the dot notation causes the ivar to be changed through the accessory rather than directly.
Until you know what you're doing, I recommend you use the dot notation (i.e.
self.propertyName = ...
). Cocoa/Obj-C does a lot with key-value coding, and while the phone SDK doesn't take full advantage of that (with things like bindings), eventually it will. Getting used to using the accessors now will save you a lot of headaches in the future.Using the accessor methods also give you the opportunity to override them and provide more functionality should you need to. By simply changing the value of the ivar, you rob yourself of this capability.