self.variable and variable difference
This question already has an answer here:
Difference between self.var and simply var
- CGContext Erase Error
- API to grab other application's window/document name
- Best way to set a retained property to a newly created object
- how to compare two optional NSArrays in Swift
- What is the best way to store an array of objects with cocoa?
- Using NSPredicate to filter an NSArray based on NSDictionary keys
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.
- How to use UIEdgeInsets property in Button in iphone
- UITableViewAutomaticDimension not working in Xcode 6.3
- AVPlayer – Fast Backward / Forward Stream
- Animate popoverContentsize when pushing navigation view controller in popover on iPad
- create quarter transparent hole at right bottom on overlay UIView
- camera overlay change with bearing and elevation
- UICollectionViewCell to UIButton Focus in tvOS
- After updating Xcode to IOS 9, app stucks on splashscreen and initial viewcontroller is not loaded
- When converting a project to use ARC what does “switch case is in protected scope” mean?
- How to reverse order of items from Firebase
- Create a UIImage with a URL in iOS
- Swift Compiler error: 'Double' is not convertible to CGFloat
- Invalid parameter exception thrown by _UIQueuingScrollView
- Could not read from Info.plist
- how to add on touch and detect UIImageView on view so i could move it around and not adding one on top of it?