Differences and relationships between classes, objects, instance variables, member variables, and attribute variables in iOS development


Class (class): a general term for a class of elements with the same attributes and behaviors. A class is an abstract concept.
Distinguish whether it is a class or an object to see if it can continue to be subdivided.
In Objective-C, a class is a structure that represents an object type, and an object obtains various information about itself through a class. A class consists of two parts: *.h and *.m files.
The implementation part in the *.m file is the implementation part of the class, which contains various information in the class, including various instance methods or class methods.
 
Category (category): is a way to add new methods to an existing class, usually named with "class name + category name".
New instance variables cannot be added to a class. But it is possible to add properties to the category.
The main use of categories in business development is to add new methods to a class without changing the content of the original class in the source code.
 
Class extension: A class extension is a special form of classification, an extension is a classification without a name. The file is usually: "main class class name_extension identifier.h"
extension is usually defined in the .m of the file and cannot be separated. Extensions can declare instance variables, properties, and methods, but all are private methods.
 
Object (object): It is not subdivided and represents a specific thing.
In Objective-C, an object is a structure that contains a value and a hidden pointer to its class.
By instantiating a class into an object.
The instantiation methods include alloc, new, copy, factory method, singleton method, etc.
 
Instance variable (instance variable): the variable of the instance defined by the class (excluding basic data types, such as int, double, float).
 
Member variable: a variable in the code, including instance variables and basic variable types, without contact with the outside world.
Member variables are protected by default. In general, non-subclass objects cannot be accessed.
 
Property: The compiler automatically combines the set and get methods of variables, which can be read with dot syntax, used as variables, and can be contacted with the outside world. .
 
 
To distinguish the difference between instance variables and member variables:


you can see that in the interface @interface brackets are collectively referred to as "member variables", and instance variables are one of the member variables!
The English translation of the instance variable is Instance Variable (object-specificstorage) 
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**
 
In @property (description 1, description 2, description 3) (class *) varName, there are 3 descriptors that need to be filled in (you can also leave it blank and take the default value)
1. nonatomic<-->atomic
2. readwrite<-->readonly
3. retain/copy/assign Let 's
 
first introduce:
retain: It refers to assigning a pointer to a memory area to a variable, and at the same time assigning the memory area The reference counter is incremented by 1. Each time it is executed, the reference counter of the memory area is incremented by 1. When the reference counter of this area becomes 0, the memory area is released!
 
copy: It means to copy the value of the target memory area, and then open up a new memory area (new pointer) to paste the value. At the same time the variable is assigned a pointer to the new memory area!
 
assign: It means that only the pointer of the target memory area is assigned to the variable, and the reference counter of the memory area does not change!
 
1 and 2 are not explained. The retain, copy and assign in 3 all refer to. When the setter function is automatically generated, the compiler needs to identify a descriptor to generate the corresponding setter function! It should be noted that if the descriptor of the class is not added, the system defaults to the method of assigning the setter method of the variable.
In the header file .h generally has instance variables defined in {} 
  
 
:
.h
@property (automic, retain) NSString * abc;
.m
@sythesize abc;
 
//In the case of @sythesize abc; , the system will not automatically generate the instance variable "_abc", directly pass the variable name abc, that is, directly use the variable name in the assignment operation (the left side of the = sign), just assign the pointer of the memory area to the variable, which is equivalent to assgin. If It is assigned through the "dot statement" self.abc=, it depends on which one of copy, retain, and assign is defined in @property. If the above descriptor is not added, it will default to assign.
//If you don't write @sythesize abc; the system will automatically add an invisible member variable with "_" in the .h file {} by default (even if the variable name itself has "_")
//The member variables defined in the brackets (basic data types + class-generated variables), the variables inside can be directly accessed in the .m file through "variable name", self->"variable name" to the variables in the brackets, However, such assignment access can only be assign, and the reference counter of the original object will not change.
//1.@sythesize variable name; 2.@sythesize variable name=_variable name; 3. Do not write @sythesize (the variable names mentioned below refer to the variables defined in @property in the header file)
1. Member variables, instance variables are directly accessed through "variable name" or self->"variable name", and assignment (assign). self. variable name implements setter and getter methods.
2. Member variables, instance variables are directly accessed and assigned through "_variable name" or self->"_variable name". self. variable name implements setter and getter methods.
3. Member variables. Instance variables (instances generated by the system automatically adding "_" before the original variable name, member variables) are directly accessed (assign) through self->_ variable name, or variable name. self. variable name implements setter and getter methods.
 
If there is no variable defined by @property in the header file, but member variables are defined in {}, and there is no @sythesize in the implementation file, then you can directly pass self->"variable name in {}", or Directly use the "variable name in {}" to access the assignment. Such a variable does not define setter and getter functions, and can only be assigned in the way of assign.
 
//Let's analyze the writing in @sythesize again. @sythesize abc can use self.abc directly in the .m file to call the setter and getter functions of the member variable. Directly calling the member variable name abc is the pointer to access the variable. Direct assignment of variables is equivalent to the ASSIGN effect.

Guess you like

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