Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios interface iVar vs Property

In an .h file, what is the difference between:

@interface ViewController : UIViewController
@property (strong, nonatomic) UIView* myView;

And

@interface ViewController : UIViewController{
    UIView* myView;
}
like image 380
Chris G. Avatar asked Oct 16 '12 12:10

Chris G.


People also ask

What is an iVar?

An opaque type that represents an instance variable. iOS 4.0+ iPadOS 4.0+ macOS 10.5+ tvOS 9.0+ watchOS 2.0+

What is property in iOS?

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.

How do I declare iVar in Objective-C?

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 .

What are properties and instance variables in Objective-C and swift?

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.


1 Answers

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.

like image 153
iSofTom Avatar answered Sep 19 '22 13:09

iSofTom