Objective-c Object Initialization Objects Initialization

Source: Internet
Author: User

Objective-c Object Initialization Objects Initialization

1.1 Allocating Objects Assignment Object

Allocation is the process by which a new object is born.

Allocation is the process of the birth of new objects.

Sending the ALLOC message to a class causes this class to allocate a chunk of memory large enough Variables.

Sending a ALLOC message allows the class to allocate enough memory.

All your bools start out as NO;

All your ints is 0; All your floats become 0.0; All your pointers is nil;

A newly allocated object isn ' t ready for being used right away:you need to initialize it before your can work with it.

A newly allocated object is not available, and you need to initialize it.

Some languages, including C + + and Java, perform object allocation and initialization in a single operation using a Constru ctor. Objective-c splits the operations into explicit allocation and initialization stages.

Java and C + + execute allocation and initialization in one operation.

And the Objective-c separated two operations.

1.2 Initializing Objects Object initialization

Initialization takes a chunk of memory and gets it ready to become a productive member of society. Init Methods-that is, methods that does initialization-almost always return the object they ' re initializing.

Initialize it to become a member of the object. Init methods means that the object to be created is always returned.

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

1.3 Writing Initializationg Methods Write initialization method

(ID) Init {

if (self = [super init])

{

engine = [engine new];

Tires[0] = [Tire new];

TIRES[1] = [Tire new];

TIRES[2] = [Tire new];

TIRES[3] = [Tire new];

}

return (self);

}//Init

The first bit of code that runs in this statement is [Super Init]. That code lets the superclass does its initialization work.

[Super Init] allows the parent class to complete initialization work.

For classes this inherit from NSObject, calling on the superclass lets NSObject does any processing it needs to doing so that O Bjects can respond to messages and deal with retain counts.

For any class that inherits from NSObject, initialization enables the object to detail and handle retain counts.

For classes the inherit from another class, this is their chance to do their own version of Clean-slate initialization.

For classes that inherit from other classes, it has the opportunity to initialize itself.

Remember that instance variables is found at a memory location, that's a fixed distance from the hidden self parameter.

Remember that the instance variable is found in the memory location, and the fixed position is able to find the hidden self parameter.

If a new object is returned from the Init method, we need to update the self so and any subsequent instance variable reference s affect the right places in memory.

If an object returns an Init method, we need to update self to find the instance variable at the appropriate location in any of the child variables.

An Init method can return nil if there ' s a problem initializing an object.

One of the initialization methods may return nil.

Keep in mind, this is assignment affects the value of the self, the This method. It doesn ' t change anything outside the method ' s scope.

Remember that this assignment only affects self within this method. Does not affect other methods.

The test if (self = [super init]) won ' t run the body code if nil are returned from [Super Init].

This test will not run if [super init] returns to nil.

An Init method returns the object is just initialized. Since we assigned the return value of [Super init] to self, that's what we should return.

A properly initialized method returns an initialization object. Because we assign [super Init] to self, we return to self

1.4 What does when you ' re Initializing

This was the place to do your clean-slate initialization work. You assign values to instance variables and create the other objects that your object needs to do it work.

Here, you can do some work from scratch. You assign values and create other objects that you need. What does initialization need to do or look at your own needs?

1.5 Convenience initializers

In fact, it's important to remember that init methods is nothing special. They ' re just ordinary methods that follow a naming convention.

Init methods is nothing special. They are also ordinary classes.

Many classes has convenience initializers. These is init methods that does some extra work and saving you the trouble of doing it yourself.

Many classes provide simple methods. Do the extra work.

-(ID) Initwithformat: (NSString *) format, ...;

This is also the initialization method.

Objective-c Object Initialization Objects 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.