Differences and connections between member variables, instance variables, and properties in iOS

First, the relationship between member variables, instance variables, and attribute variables

@interface MyViewController :UIViewControlle
{

    UIButton *yourButton;
    int count;
    id data;
}
@property (nonatomic, strong) UIButton *myButton;
@end

  Variables declared in { } are all member variables. So yourButton, count, data are all member variables. In that case, what does instance variable mean? Instance variables are essentially member variables, but the instance is for the class, and the instance refers to the declaration of the class. { } in yourButton is an instance variable. id is an OC-specific class, essentially id is equivalent to (void *). So id data is an instance variable.

  Member variables are used inside the class and do not need to be in contact with the outside world. Because member variables do not generate set and get methods, the outside world cannot contact member variables. According to the privacy of member variables, in order to facilitate access, there are attribute variables. The benefit of a property variable is that it allows other objects to access the variable (because the set and get methods are automatically generated during property creation). Of course, you can set read-only or writable, etc., and the setting method can also be customized. So, property variables are variables used to interact with other objects.

  To sum up, it can be seen that a member variable is a variable defined in {}. If the data type of the variable is a class, the variable is called an instance variable. Because instance variables are a special case of member variables, instance variables are also used inside the class and do not need to be in contact with the outside world. This is also called a class private variable. Whereas property variables are variables used to interact with other objects.

  However, now everyone seems to not like to use member variables to define class variables, and all like to use attribute variables to define class variables. The variables that need to be contacted with the outside are defined in the .h file, and the variables used only in this class are defined in the .m file.

     First, let 's distinguish the difference between instance variables and member variables :

You can see that the parentheses in the interface @interface are collectively referred to as "member variables", and instance variables are one of the member variables!

The English translation of instance variable is Instance Variable (object-specific storage) 

The English translation of the instance is Instance (manifestation of a class), which means "the performance of the class", indicating that the instance variable should be a variable defined by the class!

Except for the basic data types int float .... etc., other types of variables are called instance variables.

**Instance variable + basic data type variable = member variable**

 Second, the attribute property in the category

  Classes are distinguished from properties added to categories, because only methods can be added to categories, not instance variables. It is often seen in the code of ios that attributes are added to the category. In this case, instance variables will not be automatically generated, and you must implement the get/set method yourself.

  Note that instance variables can be added to anonymous categories (anonymous extensions), while instance variables cannot be added to non-anonymous categories, only methods or properties (in fact, methods).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324438697&siteId=291194637