Objective-c (Initialize)

Source: Internet
Author: User

objective-c (Initialize)


Creating Objects
(programming language OBJECTIVE-C 2.0)

1. class objects and instantiation

After the definition of the class is complete, the compiler automatically generates a unique class object in memory, and the instance object is generated by invoking the class object's class method .

The class object is a factory, but also a blueprint, and the instance object is the product

Basic steps for creating objects

Creating an object using OBJECTIVE-C requires two basic steps:

Allocation (memory allocation): Dynamically allocating a memory address for a new object

Initialization (Initialize): Fill in the appropriate initial values for this memory space

Only after these two steps have been completed will the object be able to actually start exercising the function

Basic steps for creating objects

Two steps are done separately in the code, but usually we put them together once to complete:

ID anobject = [[Rectangle alloc] init];

Separating the two steps of creating an object allows us to control the memory allocation for each of these two steps

2. The return value of the initialization method

Allocating Objective-c NSObject,

Two default memory allocation methods are available:

+alloc

+allocwithzone:isa

The memory allocation method initializes the new object's

variables, and other memory allocation methods should not be replicated or altered

The value of the variable is reset to the return value of the initialization method

Typically, the initialization method initializes an instance variable of the receiver object, and then returns the object itself

The primary responsibility of the initialization method is to ensure that the object it returns does not appear with the return value of the error initialization method when it is used

Sometimes, however, the initialization method does not perform the functions it is required to perform properly

1: File access error Sometimes, the object returned by the initialization method is not the recipient object itself

2: Duplicate objects

For these reasons, the program should use the object returned by the initialization method instead of directly using the object returned by the memory allocation method

The following code is very dangerous because it completely ignores the status of the return value of the initialization method: ISA

ID anobject = [SomeClass alloc];

[AnObject Init];

[AnObject Someothermessage];

In order to initialize the new object safely, the memory allocation method and the initialization method should be used together:

ID anobject = [[SomeClass alloc] init];

[AnObject Someothermessage];

If the initialization method is likely to return nil, you will also need to do a checkpoint

Acting

ID anobject = [[SomeClass alloc] init];

if (AnObject)

[AnObject Someothermessage];

else ...

3. Implementation of the initialization method

When a new object is created, in addition to its

Variable, all of its bits in memory are placed

Sometimes, this initialization is sufficient for an object

But most of the time, you also need to give an initial value to other variables of the object to ensure that it starts to be used.

In these cases, you need to implement a custom initialization

 

Objective-c (Initialize)

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.