self.variable and variable difference
This question already has an answer here:
Difference between self.var and simply var
- Save blank file in Objective-C
- Is it possible to create a view-based NSTableView purely in code?
- Not receiving CloudKit push notifications for Custom Record Zone on the Mac
- preferredInterfaceOrientationForPresentation must return a supported interface orientation
- how to get input from UIAlertView?
- What is this double underscore in Cocoa
6 Solutions Collect From Internet About “self.variable and variable difference”
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
- bill = fred will set bill to fred
directly, without going through the
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).
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.
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.
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.
- initWithFrame : reuseIdentifier : is deprecated
- Cocoa Custom Notification Example
- UIAlertView starts to show, screen dims, but it doesn't pop up until it's too late!
- Xcode ui test: staticTexts start with
- What is Silent Push Notification? When does the device receive it?
- NSFetchedResultsController ignores fetchLimit?
- Is self retained within this Objective-C block?
- NSDictionary setValue:forKey: — getting “this class is not key value coding-compliant for the key”
- Does IOS support Active Directory in any way?
- How to animate the background color of a UILabel?
- Can a UIWebView interact (communicate) with the app?
- How to not stretch an image in UIButton
- Syntax help – Variable as object name
- Does CFHostGetAddressing() support ipv6 DNS entries?
- Why should I use a separate test host for running XCTests and how should I do that?