The differences and relationships between classes, objects, instance variables, member variables, attribute variables, and so on in IOS development __ios

Source: Internet
Author: User

Class: Class is an abstract concept that has the same attributes and behavior equivalent to a class of elements.

Distinguish between a class or an object to see if it continues to be subdivided.

In OC, a class is a struct that represents an object type, and an object obtains its own information through a class. The class is composed of two parts: *.h and *.m files.

The implemention part of the *.M file is the implementation part of the class, which contains various information within the class, including various instance methods or class methods.


Category (category): Is the way to add new methods to an existing class, usually named "Class name + category name".

A new instance variable cannot be added to a category. However, you can add attributes to the category.

The main purpose of a category in business development is to add a new method to a class without changing the original class content of the source code.


Class extension: Class extensions are a special form of categorization, and extensions are categories without names. File is usually: "Main class class name _ extension identity. h"

Extends the. m of the file that is normally defined, and cannot be separated. An extension can declare instance variables, properties, methods, but all are private methods.


Objects (object): Is not subdivided, represents a specific thing.

In OC, an object is a struct that contains a value and a hidden pointer to its class.

The class is instantiated into an object by instantiating it.

The instantiation method has Alloc,new,copy, factory method, single case method, etc.


Instance variable (instance variable): A variable of an instance that is defined by a class (excluding the base data type, such as Int,double,float).


Membership variable (member variable): A variable in code that contains instance variables and basic variable types without contact with the outside world.

The member variable defaults to protected and, in general, non-subclass objects are inaccessible.


Property: The compiler automatically synthesizes the set and get methods of a variable, can be read as a variable, and can be used in contact with the outside world.


To distinguish between instance variables and member variables:
You can see that in the interface @interface parentheses are collectively called "member variables", and instance variables are one of the member variables. The English translation of an instance variable is the English translation of Instance Variable (object-specificstorage) as Instance (manifestation of a class) that says "class expression", Indicates that the instance variable should be a variable defined by the class. Drop basic data type int float .... And so on, other types of variables are called instance variables. * * Instance variable + base data type variable = member Variable * *
Now, let's talk.Properties:
in @property (description 1, description 2, Description 3) (class *) VarName, there are 3 descriptive words that need to be filled in (or you can not fill out the default values) 1. Nonatomic<-->atomic 2. Readwrite<-->readonly 3. Retain/copy/assign
first of all, let me introduce:Retain: He refers to assigning a pointer to an area of memory to a variable and adding a reference counter of that memory area to 1. Each time the reference counter for the memory area is incremented by 1, the memory area is freed when the reference counter of the zone becomes 0.
Copy: It refers to a copy of the value of the target memory area and then opens up a new memory area (a new pointer) to paste the value. The variable is assigned a pointer to the new memory region.
Assign: It means that only the pointer to the target memory region is assigned to the variable, and the reference counter for that memory area does not change.
1, 22 points do not explain, 3 of the Retain, copy, assign are referred to, in the automatic generation setter function, the compiler needs to identify a descriptive word to generate the corresponding setter function. Note that if you do not add the description of the class, the system defaults to the setter method of the variable to take a assign approach. In the header file. h typically has a defined instance variable in {}
Example:. 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 through the variable name ABC, that is, directly using the variable name in the assignment operation (= number to the left), just the memory area of the pointer assigned to the variable, equivalent to Assgin. If it is through the "point statement" self.abc= to assign value, it is necessary to see in the @property definition of copy, retain, assign which kind of, if not add the above descriptive words, the default is assign.

If @sythesize ABC is not written, the system automatically adds an invisible member variable with "_" in the. h file {} (even if the variable name itself has "_")

Inside the parentheses are defined as member variables (basic data type + class generation variables) in which variables can be accessed directly through the "variable name", self-> "variable name" in the. m file, but such assignment access can only be assign. The reference counter for the original object does not change.

1. @sythesize variable name; 2. @sythesize variable name =_ variable name; 3. Do not write @sythesize (the variable names mentioned refer to the variables defined in @property in the header file)

1. Member variables, instance variables are accessed directly by "variable name" or self-> "variable name", and assigned (assign). Self. Variable name implements Setter,getter method.

2. Member variables, instance variables are accessed directly by "_ Variable name" or self-> "_ Variable name", and assigned (assign). Self. Variable name implements Setter,getter method.

3. member variables. Instance variables (the system automatically adds "_" to the original variable name to generate the instance, member variable), directly through the self->_ variable name, or variable name directly to (assign). Self. Variable name implements Setter,getter method.


If there are no variables defined by @property in the header file, however, there are defined member variables in {}, and there is no @sythesize in the implementation file either, you can access the assignment directly through the variable name in self-> "{}", or by using the variable name in "{}" directly. Such a variable does not define a setter,getter function and can only be assigned in a assign way.


To analyze the @sythesize in the writing, @sythesize ABC directly in the. m file using SELF.ABC can call the member variable's setter, Getter function, directly call the member variable name ABC is to access the pointer to the variable, Assigning a member variable directly is equivalent to the Assign effect.



Collated and reproduced from

Http://blog.sina.com.cn/s/blog_75830d770101g560.html





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.