How to Create a Cocoa object

Source: Internet
Author: User

CocoaIn the basic principles guideObjectThe created content is described in this article,Cocoa objectIs always divided into two phases:ObjectAllocation and initialization.CocoaFrom the virtual storage area of the ApplicationObjectAllocate enough memory. When calculating how much memory needs to be allocated,CocoaWill considerObjectInstance variables, including their types and order.ObjectClass. To proceedObjectAllocate, you needObjectClass to send alloc or allocWithZone: Message.

You can get a "generated" uninitialized class instance in the return value of the message. The alloc method uses the default virtual storage area of the application. A page-aligned memory area is used to store objects and data allocated by applications.

In addition to allocating memory, Cocoa allocates allocation messages and performs other important work:

 
 
  1. <! -- [If! SupportLists] --> § <! -- [Endif] --> set the number of objects to 1.
  2. <! -- [If! SupportLists] --> § <! -- [Endif] --> point the isa instance variable of the initialization object to the class of the object. An object class is a runtime object compiled according to the class definition.
  3. <! -- [If! SupportLists] --> § <! -- [Endif] --> Initialize all other instance variables to 0 or the type equivalent to 0, such as nil, NULL, and 0.0 ).

The isa instance variables of the object are inherited from NSObject, so all Cocoa objects have. After the isa Pointer Points to the object class, the object is integrated into the runtime view of the inheritance level and the object class and instance that constitute the program) network. The result is that the object can find all the runtime information it needs, such as the position of other objects at the inheritance level and the Protocols they follow, and the implementation location of methods that can be executed in response to messages.

The initialization process sets the instance variables of the object as reasonable and useful initial values. It also allocates and prepares other global resources required by the object, and loads resources such as files as necessary. All objects declared as instance variables should implement an initialization method-unless the default initialization of all variables set to 0 is sufficient. If an object does not implement its own initialization method, Cocoa will call the method of its closest ancestor object.

NSObject declares the init method as the prototype of the initialization method. It is an instance method and returns an object of id type. You can reload the init Method for subclasses that do not need to initialize other data. However, in the initialization phase, you need to set the initial state of the object based on external data. The only requirement is that the initialization method must start with the letter "init". Sometimes, the format rules are used to describe init ). The nil returned from the initialization method indicates that the requested object cannot be created. When creating an object, check whether the returned value is nil before processing. Once the object is initialized, initialization should not be performed. If you try to perform repeated initialization, the framework class of the instantiated Object usually produces an exception.

Implement an init... to use the method as the unique Initialization Method of the class or the specified Initialization Method of the class with multiple initialization methods, see the description in "multiple initialization methods and specified initialization methods ), there are several key steps:

 
 
  1. <! -- [If! SupportLists] --> § <! -- [Endif] --> always first calls the initialization method of the super class.
  2. <! -- [If! SupportLists] --> § <! -- [Endif] --> check the objects returned by the superclass. If it is nil, initialization cannot be performed and nil needs to be returned to the recipient object.
  3. <! -- [If! SupportLists] --> § <! -- [Endif] --> when initializing instance variables, if they are referenced by other objects, they are retained and copied if necessary.
  4. <! -- [If! SupportLists] --> § <! -- [Endif] --> after the instance variable is set to a valid initial value, self is returned, except for the following situations:
  5. <! -- [If! SupportLists] --> § <! -- [Endif] --> A replacement object must be returned. In this case, the newly allocated object is released first.
  6. <! -- [If! SupportLists] --> § <! -- [Endif] --> If initialization fails due to some problems, nil is returned.

In many ways, the dealloc method of a class echoes the init... method, especially the initialization method. The initialization method is called immediately after the object is allocated, and dealloc is called before the object is destroyed. The initialization method ensures thatObjectThe instance variable is correctly initialized, And the dealloc method ensures thatObjectAnd ensure that the dynamically allocated memory is released.

The last similarity between the two is that they must call their own superclasses. In the initialization method, first call the specified Initialization Method of the superclass; In the dealloc method, call the dealloc implementation of the superclass In the last step. The reason for this is that, unlike the initialization method, subclass should release its own instance variables before the instance variables of the ancestor class are released.

The implementation of the class factory method is to provide convenience to customers. They combine allocation and initialization in one step and return the createdObjectAnd perform automatic release. These methods are in the form of + (type) className... where className does not include any prefix ).

Summary:CocoaBasic principles guideObjectThe content of the tutorial has been introduced. I hope this article will help you!

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.