self.variable and variable difference [duplicate]

不打扰是莪最后的温柔 提交于 2019-11-26 13:47:46

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);

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.

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:

  • self.bill = fred will call the accessor setBill.
  • bill = fred will set bill to fred directly, without going through the accessor.

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

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

@synthesize myProp;

Always use the self.myProp pattern to set it.

self.myProp = newVal;

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!