Objective, objectivec

Source: Internet
Author: User

Objective, objectivec

Objective-C Object initialization

1.1 Allocating Objects

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

Allocation is the process of creating a new object.

Sending the alloc message to a class causes that class to allocate a chunk of memory large enough to hold all its instance variables.

Sending alloc messages allows the class to allocate enough memory.

All your BOOLs start out as NO;

All your ints are 0; all your floats become 0.0; all your pointers are nil;

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

An allocated object is not available yet. You need to initialize it.

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

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

Objective-C separates 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 do initialization-almost always return the object they re initializing.

Initialize and make it a member of the object. Init methods indicates 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 that statement is [super init]. That code lets the superclass do its initialization work.

[Super init] allows the parent class to complete initialization.

For classes that inherit from NSObject, calling on the superclass lets NSObject do any processing it needs to do so that objects can respond to messages and deal with retain counts.

For any class inherited from NSObject, initialization allows the object to be detailed and processed in retain counts.

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

For Classes inherited from other classes, it has the opportunity to initialize itself.

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

Remember that the hidden self parameter can be found at the memory location where the instance variables are located and fixed.

If a new object is returned from an init method, we need to update self so that any subsequent instance variable references affect the right places in memory.

If an object returns an init method, we need to update self to locate the instance variable in any sub-variable.

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

An initialization method may return nil.

Keep in mind that this assignment affects the value of self only for this method. It doesn't change anything outside the method's scope.

Remember that this assignment only affects self in this method. Other methods are not affected.

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

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

An init method returns the object that was just initialized. Since we assigned the return value of [super init] to self, that's what we shoshould return.

A correct initialization method returns an initialization object. Because [super init] is assigned to self, self is returned.

1.4 What to Do When You're re Initializing

This is 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 its work.

Here, you can start from scratch. You allocate values and create other objects you need. The initialization depends on your own needs.

1.5 Convenience initializers

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

There is nothing special about init methods. They are also common classes.

Classes have convenience initializers. These are init methods that do some extra work, saving you the trouble of doing it yourself.

Many classes provide simple methods. Do additional work.

-(Id) initWithFormat: (NSString *) format ,...;

This is also the initialization method.

 

 

 

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.