Underscore prefix on property name?
How does an underscore in front of a variable in a cocoa objective-c class work?
Can anyone point me to an explanation of the use of underscores, I have always assumed that they are used to highlight that you are accessing the iVar
[_window release]; rather than accessing the iVar via a setter/getter method
[[self window] release]; or
[self.window release]; I just want to verify that my understanding is correct.
@property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UILabel *markerLabel; @synthesize window = _window; @synthesize markerLabel = _markerLabel;
- UIButton as UINavigationbar button
- Want to change a particular color inside an image with another color - iPhone
- Xcode warning when using MapKit and CoreLocation
- UITextField: move view when keyboard appears
- Unable to link GoogleAnalytics 3.01 with XCode 5 (missing required architecture x86_64)
- NSURLConnection - Disable the authentication challenge response mechanism
2 Solutions Collect From Internet About “Underscore prefix on property name?”
The use of an underscore for ivar names is a convention first used by Apple to differentiate between an actual ivar and a property. Many people have since adopted this convention.
The reason this is done is to prevent the mistake of assigning a new value to an ivar instead of to the actual setter:
myIvar = newValue;
self.myIvar = myValue;
If you accidentally use the top example, you could cause a memory leak. The underscore prevents you from making that mistake.
It’s a convention, and your understanding sounds fine.
Another merit of this convention is that you can’t accidentally access the ivar without using the setter, because
view = viewController.view ; // usually bad
will not compile if the ivar is
_view instead of
I usually don’t declare the ivar, though, and just use
@synthesize. Without an explicit ivar declared, the compiler (at least a recent one) automatically generates the ivar (with the same name.) So the code above compiles without problem, and doesn’t serve as a guard against not using the setter, but you can do away with one line of code…. which might be a bad thing to aim for and I don’t recommend it to you :p
- How to Covert struct with an Array of string to NSData and vice versa Swift
- Printing optional variable
- Apple and stripe : Merchant id/apple pay certificate could not seen in xcode
- Swift: Firebase: How to ensure no one can access my db except my app
- Exception:Google Maps SDK for iOS must be initialized via prior to use
- How to convert JSON to Object
- iOS swift NumberFormatter decimal style localization
- Can my class override protocol property type in Swift?
- NSDictionary to UITableView
- How to upload video to Vine programmatically on iOS?
- Updating label if value in singleton changes
- iOS/Cocoa – Design Pattern for Data Model <-> Controller
- iPhone :UITableView CellAccessory Checkmark
- CLLocationManager while app is in background state
- NSPredicate to compare Int32