OBJECTIVE-C Basic Learning Note-Object initialization

Source: Internet
Author: User

There are two ways of creating new objects in obj: [classname new] and [[ClassName Alloc] init]. The two methods are equivalent, and the Cocoa Convention is to use Alloc and init.

1. Assigning objects:

Allocation is a new object birth process that obtains a piece of memory from the OS and specifies the location of the instance variable that holds the object. The Alloc method also initializes the memory area to 0. BOOL initialized to no,int initialized to 0,float initialized to 0.0, the pointer is initialized to nil.

Init is then initialized before it can be used, and C + + and Java use constructors to perform the assignment and initialization of objects in a single operation, objective-c two operations apart.

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

-(ID) init

{

if (Self=[super init])

{

Engine=[engine new];

Tires[0]=[tire new];

}

}

-(ID) init return value, ID can table different objects. Init can accept parameters and may decide that objects that return another class might be more appropriate. For example, a new string is produced from a very long string.

The IF (Self=[super init]) First is [Super Init], which enables the superclass to do its own initialization work. Causes the superclass to perform whatever action is required so that the object responds to the message and processes the retention counter. The distance from the memory location where the instance variable resides to the hidden Self is fixed, and if the Init method returns a new object, you need to update self so that the reference to the subsequent instance variable is mapped to the correct memory location, Self=[super init] Assignment is the function, Affects only the value of self in the Init method and does not affect content outside of the init range. When initializing an object error, nil is returned. if (self=[super init]) Typical C-style, generally do not use Self=[super init] if (self) ...

Assign values to instance variables and create the engine and tires objects required for car in the initialization of car. You can create all the required objects at once so that the car class can be used immediately after [[Car alloc] init], or you can reserve a location for the engine object and the tire object, and then create the object when the caller needs it, lazy evaluation.

3. Facilitation of initialization functions.

To reduce the hassle of work, many objects have multiple methods at the beginning of init. NSString Class for 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 auto-match feature provided by Xcode is very useful, and when the input init presses the ESC key, all functions that can be matched are displayed.

All objects created using the Alloc,copy,new method need to be freed after completion. [STR release];

NSString *str = [NSString stringwithformat:@ "%.1f", 20.0]; STR is automatically released here, and the string object is also cleaned up when the auto-free pool is destroyed.

In general, the main function first creates an auto-free pool that provides a shelter for automatically freed objects while waiting for the auto-free pool to be destroyed:

NSAutoreleasePool *pool;

Pool = [[NSAutoreleasePool alloc] init];

At the end of the program, 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 write our own convenient initialization functions, and we can write more than one. However, you must specify an initialization method for the specified initialization function, and all initialization methods for that class use the specified initialization function to perform the initialization operation. Subclasses use the specified initialization functions of their superclass to implement the initialization of the superclass. If you construct an initialization function, you need to call the specified initialization function of the superclass in your own specified initialization function. If you do not specify an initialization function, then the class subclass may need to override all its initialization functions.

OBJECTIVE-C Basic Learning Note-Object initialization

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.