In an .h file, what is the difference between:
@interface ViewController : UIViewController
@property (strong, nonatomic) UIView* myView;
And
@interface ViewController : UIViewController{
UIView* myView;
}
An opaque type that represents an instance variable. iOS 4.0+ iPadOS 4.0+ macOS 10.5+ tvOS 9.0+ watchOS 2.0+
Property attributes are special keywords to tell compiler how to generate the getters and setters. Here you specify two property attributes: nonatomic, which tells the compiler not to worry about multithreading, and retain, which tells the compiler to retain the passed-in variable before setting the instance variable.
For a private/protected variable, use iVar; for a public variable, use property. If you want to use the benifit of property attributes for a private variable, like retain, nonatomic etc., declare the property in the implementation file as a private property. For an iVar, you can use @private , @protected and @public .
Therefore, as a fundamental principal of object-oriented programming, instance variables (ivars) are private—they are encapsulated by the class. By contrast, a property is a public value that may or may not correspond to an instance variable.
The first one is the declaration of a property, whereas the second is only a ivar. A property is the automatic declaration of a getter and a setter for an ivar, but if there is not ivar (like in your first example) the property will create the ivar too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With