Objective-C diary producer object initialization

Source: Internet
Author: User

There are two methods to create an object in OC:

[Class Name New]

[[Class name alloc] int]


Allocation and initialization are two separate operations: To do nsobject class method alloc allocates a memory area for the object and clears it. The instance method init is used to obtain an object and run it.

1. Allocate objects

Allocation: allocaton[, Using L yaou'kei yaoyun N]N. allocation, configuration, and placement:Obtain a piece of memory from the operating system and specify it as the location where the object instance variables are stored.

Alloc: When a class sends an alloc message, it allocates a large enough memory for the class to store all the instance variables of the class. At the same time, the alloc method allocates the memory initialization value to 0. For example, all bool type variables are initialized to no, all int types are initialized to 0, all float type variables are initialized to 0.0, and all pointers are initialized to nil.

Note:

The object must beAllocation before InitializationBefore use

Ii. Initialization object

1. initialize Initialization [I, ni Yun Lai 'zei Yun N,-li 'Z-]N. Initialization; assign an initial value: Obtain a piece of memory from the operating system for Object Storage (initialization)

Usage:
Car * car = [[Car alloc] init];

2. Compile the initialization method

-(ID) Init {If(Self =[Super init]) {Engine= [EngineNew]; Tires [0] = [TiresNew]; Tires [1] = [TiresNew]; Tires [2] = [TiresNew];}Return(Self );}

Note:

Which of the following statements is currently running?[Super init]It is used to enable the superclasses to complete their own initialization work, and then enable the subclass to implement initialization (if[Super init]Not nil ). Finally[Super init]The return value is assigned to self.

Remember: This assignment operation will only affect the value of self in the init method and will not affect any content other than this method.

 

Iii. initialization functions

Definition:

Specify the initialization function: Generally, the initialization method that accepts the most parameters will eventually become the specified initialization function.

Rules:

1. You do not need to create initialization Function Methods for your own classes.

2. If you construct an initialization function, you must call the specified initialization function of the super class in your own specified initialization function.

3. If there are more than one initialization function, select one as the initialization function. The selected method should call the specified initialization function of the superclass.

Iv. Summary

In the initialization method, you need to call your own specified initialization function or the initialization function specified by the super class. You must assign the value of the superclass initialization function to the self object and return it to the value of the initialization method. A superclass may decide to return a completely different object.

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.