This share is intended for partners who are interested in developing iOS, or are already working with iOS developers. If you have a high interest in iOS development, you can work with me on iOS development, learn together, and make progress together. If you are 0 basis, it is recommended that you first read my previously shared iOS development minutes to finish the C language series, and then at the beginning Objective C language learning, if you encounter problems can also discuss with me, in addition will be free to share their own collation of about 400G iOS learning video and learning materials, are dry! Can Sina Weibo private messages? Focus on geek James, look forward to working with you to learn and discuss together!! Due to the limited time, every day in the work of the study to share, inevitably there are shortcomings, but also hope that the way the big God!
Made by: Geek James
One, @property compiler directives
[email protected] concept
(1) First @property is the compiler's instruction
(2) compiler directives are used to tell the compiler what to do, in order to save duplicate code and provide programmer code
(3) @property the accessor (Getter/setter) method used to tell the compiler to declare member variables in the declaration file
(4) The advantage is that we do not manually write getter and setter methods cumbersome code
[email protected] Use
(1) in @inteface, a declaration used to automatically generate a setter and getter.
with @property int age, you can replace the following two lines
- (int)age; // getter- (void)setAge:(int)age; // setter
[email protected] writing steps
(1) Write a @property between @inteface and @end.
(2) After @property write the property name that needs to generate the Getter/setter method declaration, note that the property after @property is not required because the attribute in the Getter/setter method name is not required. and the @property and attribute names are separated by a space
(3) Tell the data type of the property that needs to be generated between the @property and the attribute name, noting that both sides need to be separated by an overhead grid.
[email protected] Enhanced features
Since Xcode 4.x, @property can generate both a setter and a getter declaration and implementation method.
(1) By default, implementations in the setter and getter methods go to the member variables that begin with the underscore _.
@interface person : nsobject { @public int _age; int age;} @property int age; @end int Main (int argc, const char * argv[]) {person *p = [person new]; [P setage:30 ]; nslog (@ "age =%i, _age =%i" , P->age, p->_age); return 0 ;}
(2) If there is no automatically generated member variable, the auto-generated member variable is a private variable, declared in. m, cannot be viewed in other files, but can be viewed in this class
@property only generates the simplest Getter/setter method without data judgment.
setAge:-10];NSLog(@"age = %i", [p age]);
(3) If we need to judge the data, we need to rewrite the Getter/setter method.
(4) If the setter method is implemented manually, the compiler will only automatically generate getter methods
(5) If the getter method is implemented manually, the compiler will only generate the setter method automatically.
(6) If the setter and getter methods are implemented manually, the compiler will not automatically generate a non-existent member variable
[email protected] modifier
ReadOnly only the setter method is generated and no Getter method is generated
ReadWrite both generate getter and generate setter method (default)
@property (readonly) int age;
Specifies the method name of the generated method
Getter= your custom Getter method name
Setter= The Setter method name you defined (note that the setter method must have:)
If a property is provided with a Getter/setter method, then we call this property a readable writable property
If only the getter method is provided, then we call this property a read-only property
If only the setter method is provided, then we call this property a write-only property
If neither getter nor setter method is provided, then we call this property a private property
format:
@property (property modifier) data type variable name;
ReadWrite: Represents both Getter method generation and setter method
Third, @synthesize
[e-mail protected] concept
(1) @synthesize is the compiler's directive
(2) The compiler directive is used to tell the compiler what to do!
(3) @synthesize accessor (Getter/setter) method used to tell the compiler to implement member variables in the implementation file
(4) The advantage is: We do not manually write Getterr and setter method cumbersome code
[email protected] basic use
With @synthesize age = _age, you can replace
- (int)age{ return _age;}- (void)setAge:(int)age{ _age = age;}
[email protected] writing steps
(1) Write a @synthesize between @implementation and @end.
(2) Write the same property name as in @property after @synthesize so that @synthesize will copy the @property generated to @implementation
(3) Since the Getter/setter method implementation is to pass the passed parameter to the property and get the value of the property, write the value of the @synthesize attribute to assign the passed-in value to WHO and which property to return, and connect with an equal sign.
[email protected] watch out.
(1) @synthesize age = _age;
Member variables are accessed in setter and getter implementations _age
If the member variable _age does not exist, a @private member variable is automatically generated _age
@synthesize age;
(2) Setter and getter implementations will access @synthesize after the same name member variable age
If the member variable age does not exist, a @private member variable is automatically generated.
Multiple attributes can be @synthesize by one line, and multiple attributes are concatenated with commas.
third, static type and dynamic type
1. Definition of static type and dynamic type
(1) A static type, when a pointer variable is defined as an object of a particular class, uses a static type, and at compile time knows the class to which the pointer variable belongs, which always stores objects of a particular class.
(2) dynamic type, a feature in which the program does not determine the class to which the object belongs until it executes
2.id Data Types
(1) ID is a data type and is a Dynamic data type
Since it is a data type, it can be used to:
-Define Variables
-As arguments to functions
-As the return value of a function
By default, all data types are static data types
3. Characteristics of static data types
(1) The type of the variable is known at compile time,
(2) know which properties and methods are in the variable
(3) These properties and methods can be accessed at compile time, and if a variable is defined by a static data type, the compiler will error if you access properties and methods that are not part of the static data type.
4. Characteristics of Dynamic Data types:
At compile time, the compiler does not know the true type of the variable, but only when it is run to know its true type.
And if you define a variable through a dynamic data type, if you access properties and methods that are not part of the Dynamic data type, the compiler will not error
id == NSObject * 万能指针 id和NSObject *的区别: NSObject *是一个静态数据类型
You cannot call a subclass-specific method by defining a variable with a static data type
You can call a subclass-specific method by defining a variable with a dynamic data type
A variable defined by a Dynamic data type can call a private method
Disadvantage: Because the Dynamic Data type can call any method, it is possible to call the method that does not belong to itself, and compile without error, so it may cause runtime errors
Scenario : polymorphic, you can reduce the amount of code and avoid calling subclass-specific methods that require coercion of type conversions.
5.instancetype
(1) Instancetype = = ID = = Universal pointer = = Point to an object
(2) ID cannot judge the real type of the object at compile time
(3) Instancetype can judge the true type of an object at compile time.
(4) ID and instancetype in addition to a compile-time do not know the real type, one at compile-time know the true type, there is a difference
(5) ID can be used to define a variable, can be a return value, can be used as a formal parameter
(6) Instancetype can only be used as return value
Note: Use the custom construction method later, the return value uses Instancetype as far as possible, do not use the ID
Iv. Construction Methods
1.init Method
After the object has been created, the member variable will have some default values to override the Init method immediately.
To override the Init method format:
- (instancetype)init { self = [super init]; if (self) { //Initializeself. } returnself;}
2. Construction method Use note
(1) A subclass has member variables that include its own member variables and member variables inherited from the parent class, and the member variables that inherit from the parent class should be initialized first when overriding the constructor method.
principle: Initializes the parent class first, and then initializes the subclass.
First call the constructor method of the parent class [Super Init];
The initialization of the member variables within the subclass.
Never write self = [super init] as self = = [Super init]
Overriding the purpose of the construction method: In order for the object method to be created, the member variable will have some fixed value.
3. Custom Construction Methods
Sometimes only by rewriting the constructor method (the initialization method), the requirements are not met. For example, it is not possible for a class to have the same age for all students, and we need to be able to pass the student's age when creating a student. This is when you need to customize the constructor (initialization function)
4. Specification of custom construction methods
(1) must be an object method, starting with a minus sign
(2) The return value is generally instancetype type
(3) Method name must start with Initwith
Instance:
. hdeclaring files @interface Person: nsobject @property intAge@property NSString*name;when you want the object to be created with some specified values, you can use a custom construction method- (ID) Initwithage: (int) age;-(ID) Initwithname: (NSString*) name;-(ID) Initwithage: (int) Age Andname: (NSString*) name;@end. MImplementation file-(ID) Initwithage: (int) age{if( Self= [SuperInit]) {_age = age;}return Self;} - (ID) Initwithname: (NSString*) name{if( Self= [SuperInit]) {_name = name;}return Self;} - (ID) Initwithage: (int) Age Andname: (NSString*) name{if( Self= [SuperInit]) {_name = name;}return Self;}
5. Inheritance of custom methods
(1) The parent class private variable cannot be accessed in the child class.
(2) The parent class's property is given to the parent class's method to handle
(3) Custom construction methods must start with Intiwith, and ' W ' must be capitalized
6. Custom class factory Methods
What is a class factory method:
Class methods for quickly creating objects, which we call class factory methods
The class factory method is primarily used to allocate storage space to objects and initialize this storage space
Specification:
1. Must be class method +
2. The method name begins with the name of the class, with the first letter lowercase
3. There must be a return value, the return value is Id/instancetype
Instance:
.h 文件声明@interface Person : NSObject@property int age;+ (instancetype)person;+ (instancetype)personWithAge:(int)age;@end
.m文件实现+(instancetype)person{return [[Person alloc]init];}+ (instancetype)personWithAge:(int*p = [[Person alloc]init];p.age = age;return p;}
7. Inheritance of class method factory methods
Note:
(1) Any custom class factory method, creating an object in a class factory method must not be created with the class name.
(2) Be sure to use self to create
(3) Self in the class method represents the class object, exactly which class object is represented?, who calls the current method, self represents who.
Instance code:
. h file
@property int age;
+ (instancetype) person;
+ (Instancetype) personwithage: (int) age;
.m文件+(instancetype)person{return [[self alloc]init];}+(instancetype)personWithAge:(int)age{person *p = [[self alloc]init];p.age = age;return = p;}
the nature of class and the initiation process
(1) The nature of the class is actually an object (class object)
(2) The first time the class is used in the program is created, only one copy of the entire program.
(3) Each subsequent use is this class object, which is always present when the program is running.
(4) A class object is a data structure that stores basic information about a class: The class size, the class name, the version of the class, the inheritance hierarchy, and the mapping table for the message and function.
(5) Class objects represent classes, class types, object methods belong to class objects
(6) If the recipient of the message is the class name, the class name represents the class object
(7) Instances of all classes are generated by class objects, and the class object modifies the value of the instance's Isa to its own address, and each instance of Isa points to the class object of that instance
vi. Relationship between OC instance object class object meta-Object
(1) Each object is an instance of a class.
(2) Each object has a pointer to the object named Isa, which points to its class.
(3) Each class describes the characteristics of a series of its instances, including a list of member variables, a list of member functions, and so on.
(4) Each object can accept the message, and the message list that the object can receive is saved in the class it corresponds to.
Copyright NOTICE: This article for Bo Master original article, in order to be able to promote each other, learn from each other, please follow Sina Weibo: Geek James
iOS Development core Language objetive c--compiler directive @[email protected] Custom construction method and class factory