Objective-c Study Notes-Part 3-allocate memory for the object and initialize it

Source: Internet
Author: User

To create an object, follow these steps:
1. dynamically allocate memory for new objects.
2. Associate the newly allocated memory with related values.

After both steps are completed, the object creation process is complete. Although two steps are completedCodeTime
Generally, one line of code is used:
Id anobject = [[rectangle alloc] init];

Separating these two steps gives us more options.

The alloc and allocwithzone methods in the Base Object nsobject provide two allocation methods.
These two methods do not need to be overwritten in the subclass.

They allocate enough memory for member variables.

Specific steps:
1. Initialize an ISA instance and point to class object.
2. Set other member variables to zero.

Objects must be further initialized before they can work normally.

The initialization method starts with "init", such as initwithframe.

About returned objects:
If an object is not correctly initialized, The alloc method may return an empty object.

Therefore, to ensure that the final result is a valid and workable object, make necessary checks:

Id anobject = [[someclass alloc] init];

If (anobject)

[Anobject someothermessage];

Else

...

Conventions of several initiators:
1. The initialization method starts with init.
2. the return value of the initialization method should be ID.
3. The custom initialization method must call the basic initialization method to complete the underlying action.
4. The newly created object must be referenced to the self pointer.
5. assign a value to the member variable in the initialization tool using a direct value instead of an accessor method.
6. The initialization tool returns the self pointer or nil.

Example:
-(ID) Init {
// Assign self to value returned by Super's designated initializer
// Designated initializer for nsobject is INIT
Self = [Super init];
If (Self ){
// Use if to avoid initialization failure.
Creationdate = [[nsdate alloc] init];
}
Return self;
}

When the initialization object fails:
Execute the release () method and return Nil

For example:
-(ID) initwithimage :( nsimage *) animage {

 

If (animage = nil ){

[Self release];

Return nil;

}
...

The initialization tool only initializes its own member variables. The member variables of the parent class are initialized by self = [Super init ];

The designated initializer of each class should be ensured that it will call the designated initializer of the parent class

If the user calls the init method without any parameters, the default parameter should be used inside the init method to call other overload methods of the init method.

Some Classes put memory allocation and initialization together, which are called method initialization methods.

Two references:
Memory Management:
Http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-142945

Memory Management programming guide:
Http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html#//apple_ref/doc/uid/10000011i

Benefits of combining initialization and memory allocation:

1. The memory allocation requires initialization to provide some additional information.
2. Avoid binding the memory to the new object but not using it. For example, if you have uploaded a name, but an object with the same name already exists, then the existing object is returned,
the newly generated object is released after it is not used, or you can imagine a singleton mode, you must initialize and allocate memory to a new object only when the instance object does not exist. Otherwise,
the existing object should be returned.

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.