Objective-C basic learning notes -- object initialization

Source: Internet
Author: User

Bibliography: Learn objective-C on the Mac

There are two ways to create a new object in OBJ: [classname new] and [[classname alloc] init]. The two methods are equivalent. Cocoa uses alloc and init.

 

1. Allocation object:

Allocation is the birth process of a new object. It obtains a piece of memory from the OS and specifies the location where the instance variables of the object are stored. At the same time, the alloc method initializes all the memory areas to 0. Bool Initialization is no, int Initialization is 0, float Initialization is 0.0, and pointer Initialization is nil.

The init can only be used after initialization. In C ++ and Java, constructors are used to allocate and initialize objects in a single operation. Objective-C separates the two operations.

 

2. Car * car = [[Car alloc] init];

-(ID) Init

{

If (Self = [Super init])

{

Engine = [engine new];

Tires [0] = [Tire new];

}

}

-(ID) the return value of init. IDS can be different objects in the table. Init can accept parameters and may determine that it is more appropriate to return objects of another class. For example, produce a new string from a long string.

If (Self = [Super init]) first involves [Super init], so that the superclass can complete its own initialization. Enables the superclass to perform any operation required so that the object can respond to the message and process the reserved counter. The distance from the memory location where the instance variable is located to the hidden self is fixed. If the init method returns a new object, update self, so that the reference of instance variables can be mapped to the correct memory location. Self = [Super init] value assignment only affects the self value in the init method, content outside the init range is not affected. When an error occurs during initialization of an object, Nil is returned. If (Self = [Super init]) is a typical C style, generally not using self = [Super init] If (Self )...

Assign a value to the instance variable during car initialization and create the engine and tires objects required by the car. You can create all required objects at a time so that the car class can be used immediately after [[Car alloc] init]. You can also reserve a location for the engine object and tire object first, creates an object when the caller needs it. Lazy evaluation is used ).

 

3. Convenient initialization function.

To reduce the workload, many objects have multiple methods starting with init. The nsstring class is used as an example:

Nsstring * emptystring = [[nsstring alloc] init];

Nsstring * STR = [[nsstring alloc] initwithformat: @ "% d or % d", 2,33];

Nsstring * STR = [[nsstring alloc] initwithcontentsoffile: @ "/tmp/1.txt"];

The automatic matching function provided by xcode is very useful. After you press the ESC key of init, all functions that can be matched are displayed.

 

All objects created using the alloc, copy, and new methods must be released after they are used. [STR release];

Nsstring * STR = [nsstring stringwithformat: @ "%. 1f", 20.0]; STR can be released automatically. When the pool is released, the string object is also cleared.

Generally, the main function first creates an automatic release pool to provide a place for automatically released objects when they are waiting for the automatic release pool to be destroyed:

NSAID utoreleasepool * pool;

Pool = [[NSAID utoreleasepool alloc] init];

When the program ends, the pool is released and the release message is sent to all objects in the pool:

[Pool release];

 

4. Specify the initial function

We can compile convenient initialization functions by ourselves and write multiple functions. However, you must specify an initialization method as the specified initialization function. All the initialization methods of this class use the specified initialization function to perform the initialization operation. The subclass uses the specified initialization function of its superclass to initialize the superclass. If you construct an initialization function, you need to call the specified initialization function of the super class in your own specified initialization function. If the initialization function is not specified, the subclass of this class may need to override all its initialization functions.

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.