IOS learning 12th-day notes (class initialization), ios 12th-day

Source: Internet
Author: User

IOS learning 12th-day notes (class initialization), ios 12th-day

Knowledge points of IOS Learning (oc language)

I. class initialization
1) init initialization method (constructor): generally called together with alloc, used to initialize member variables.
2) id type: equivalent to void * in C, which can point to any object and cannot be added *, similar to generic type in. net or java.
3) The initialization method with parameters (custom initialization method) is an instance method and must begin with initWith.
Example:-(id) initWithName :( NSString *) name andPrice :( float) price andPage :( int) page;
Book * b3 = [[Book alloc] initWithName: @ "iOS development" andPrice: 100 andPage: 600];
4) during class initialization, you must determine whether the parent class object has been initialized successfully. Only when the parent class object has been initialized successfully can you assign values to the class members.
Three-Step initialization principle:
1) Call the init method of the parent class.
2) initialize your own member variables.
3) return the self object.
Instance code:

1-(id) init 2 {3 // call the method of the parent class to initialize the member variable inherited from the parent class 4 // super is actually a compiler symbol, method 5 self = [super init]; 6 if (self! = Nil) {// nil is equivalent to NULL in c. If the parent class is initialized successfully, you can continue the operation (initialize the member variable) 7 _ name = @ "myBook "; 8 _ page = 300; 9 _ price = 50; 10} 11 return self; // return the current object 12}

5) Introduction to the dot syntax. The essence of the dot syntax is the call of a method. The method called by the dot syntax can only be an instance method of one parameter.
6) @ property declares the property. The set and get methods of the corresponding property are automatically generated. The function is equivalent to the declaration of the following two methods:
1.-(void) setName :( NSString *) setname;
2.-(NSString *) getname;
7) Description of self: In the instance method, self represents the instance object currently calling this method, and in the class method, self represents the Class Object of the current class.
8) @ synthesize the synthesis method body automatically generates the method body of the elements corresponding to the property. @ synthesize name = _ name indicates that the method is the operator member Variable _ name. If _ name does not exist,

A member Variable _ name is automatically created. To use @ synthesize, @ property is required, but @ property does not have @ synthesize.
9) @ property can declare multiple attributes of the same data type at the same time. For example, @ property (nonatomic, assign) int age, ID.
10) @ property modifier introduction:
1. readonly: Read-Only. Only the getter method is generated. No setter method is used.
2. readwrite: read/write. The setter and getter methods are generated. The default value is readwrite.
3. nonatomic: Data Synchronization is not required and the efficiency in a single-threaded application is high.
4. atomic: thread synchronization is required and the efficiency is low. The default value is
Memory Management:
5. assign: direct value assignment, which is generally used for basic types such as int, float, double, and long.
6. retain: generally used for objects
7. copy: generally used as a string
8. The default values are readwrite, atomic, and assign.


Ii. Inheritance
1) After the subclass inherits the parent class, the subclass can use all non-private attributes and methods that own the parent class, And the subclass can also override the methods of the parent class.
2) The method name, return value, and parameters must be consistent with the parent class method when the parent class method is rewritten.

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.