iOS Development Learning notes-creating and initializing objects

Source: Internet
Author: User

creation and initialization of objects in Objective-c
(Allocating and Initializing Objects)
(the main content comes from the "allocating and Initializing Objects" of Apple's ebook "The Objective-c Programming Language"). Ebooks can be downloaded from the ibooks store. The IOS Developer Library also has a document with the same name, but the content is different. )


creating objects in Objective-c is a two-step scenario:
1 The memory required to open an object, allocating object
2 assigns an initial value to each member of the object, Initializing

This step of opening up memory is simple. In general, all classes in objective-c inherit from NSObject. NSObject defines two methods--alloc and Allocwithzone:. Subclasses do not have to overload and modify both methods. Direct use will open up the memory needed to create an object of your own subclass.

important work in the second step.

methods for initializing an object typically start with init, such as a parameterless Init method, or a initWithFrame: method. The NSObject class defines the Init method. In general, subclasses need to define their own initialization methods according to their own needs.

object returned by the initialization method (return value)
It is important to note that the initialization method (init ... The return value of the method does not necessarily correspond to the same type of object as the receiver (or the caller of the C-language). If the initialization fails, the returned object is nil. For example, depending on a file to initialize the object, if the open file fails, then the returned object is nil.
The following code is dangerous:
id aobject = [someclass alloc];
[Aobject init];//The return value here is not necessarily aobject,
[Aobject someothermessage];//The receiver that should actually be someothermessage should be the return value of init, not aobject
the correct wording is as follows:
id aobject = [SomeClass alloc] init];
[Aobject someothermessage];


places to be aware of when implementing initialization methods
~ The name of the method should start with Init
~ The return value type of the method should be an ID
~ designated initializer needs to be called in the implementation (what is mentioned later in designated Initiailizer). A class has many initialization methods, one (or some) called designated initializer (translated into?). "Specified initialization method"? )。 When you implement the designated initializer of a subclass, you must call the designated initializer of the parent class. When implementing other initialization methods for subclasses, you need to call your own designated initializer directly or indirectly.
NSObject's designated initializer method is init.
~ do not use the accessor method when setting member variables in the initialization method (access methods).
~ Assign the return value of the initialization method to self.
~ returns self, or nil if initialization fails.

processing when initialization fails
If the failure returns nil, it is also necessary to call the release method on self.
-(ID) init {
Self = [super init];//Assign a value to self; Call Super designated initializer
if (self) {//Only Super's Init return value is not nil to do the following things
thedate = [[NSDate alloc] init];
  }
return self ;//If failure returns, the value of self is nil, and nil is returned
}
Another example:
-(ID) iniwithimage: (Nsimage *) aimage {
if (aimage = = nil) {
[self release];//If there is a problem with the incoming parameter, call the release method of self
return nil;
  }
Self = [Super init_xxx:yyy];
if (self) {
image = [Aimage Retian];
  }
return self;
}



subclasses need to ensure that all initialization methods inherited from the parent class will work correctly.
For example, Class A defines the Init method, and subclass B defines the Initwithname method. Class B ensures that the INIT message can also initialize class B correctly. One simple approach is that Class B's Init calls the Initwithname method of Class B to implement itself:
-init {
return [self initwithname: "Default"];
}


The designated Initializer
the designated initializer of a class ensures that all inherited members are initialized (by calling the Super method), while doing most of the other work. Other initialization methods of this class call this designated initializer.
the designated initializer method of a class must call the parent class's designated initializer instead of the other initialization methods of the parent class. If you do not do this, it may cause a dead loop.
(see ebook for specific examples)


Merge open memory and initialize memory two steps (Combine allocation and initialization)
Some classes provide methods to implement both the memory opening and the memory initialization steps in these methods. These methods are called convenience constructors (convenient constructors). The general form is +classname ....
For example, Nsarray provides:
+ (ID) array;
+ (ID) arraywithobjects: (ID) firstobj, ...;
sometimes these methods are useful. For example, + (ID) arraywithobjects: (ID) firstobj, ..., only know the number of parameters to know how much memory should be allocated. Therefore, it is not possible to separate the two steps of opening up memory and initializing memory.
look at the following example (singleton), if the object already has a direct return to an existing object, there is no need to open up memory:
+ (Soloist *) Soloist {
static Soloist *instance = nil;//note:static
if (instance = = Nil) {
instance = [Self alloc] init];
  }
return instance;
}
(The return type is soloist *, not an ID, which is appropriate in this case)

iOS Development Learning notes-creating and initializing objects

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.