Constructors and destructors for the Objective-c class

Source: Internet
Author: User



constructor function:



Objective-c is an object-oriented language, and when we define a class in objective-c, we always provide an initialization method, which is commonly written by everyone:


- (id) init {
// call the parent class method, an instance of this class, involving runtime, conflict prevention, can initialize some data in the constructor, etc
The self = [super init];
If (self) {
The self. The name = @ "XXX";
}
Return the self;
}

- (id) initWithString: (aString nsstrings *)
{
[the self init];
The self. The name = aString;
}

- (id) initWithImage: (aImage UIImage *)
{
[the self init];
Self. Image = aImage;
}


This is a simple code, but there are a lot of things to think about:



1. Why should I invoke the initialization method of the parent class through [super Init] and what is being done in the initialization method of the parent class?



First of all, we know the concept of object inheritance, a subclass inherits from the parent class, but also to implement all the functions of the parent class, this is the is-a relationship, such as the dog is a mammal, then the dog must have the characteristics and functions of mammals. Therefore, in the initialization method of the subclass, you must first call the parent class's initialization method to implement the initialization of the parent class related resources. For example, when we initialize a dog, we must first initialize the Mammal object and give the result to the dog, so that the dog satisfies the characteristic of the mammal.



Typically, under iOS, all classes inherit from NSObject, and NSObject's Init method is simple, which is return self. When the initialization of the parent class is complete, that is, self is not nil, you can begin the initialization of the subclass.






In object-oriented programming, if you write a class without a constructor, the class can compile and work perfectly. If the class does not provide an explicit constructor, the compiler provides you with a default constructor function. In addition to creating the object itself, the only function of the default constructor is to call its superclass's constructor. In many cases, this superclass is part of the language framework, such as the object class in Java and the NSObject class in Objective-c.



In either case, having at least one constructor in a class is a good programming practice, and if there are attributes in the class, it is often good practice to initialize those properties.






Destructors



Destructors Dealloc functions that are called before objects are destroyed from memory


- (void) dealloc
{
The // destructor is called automatically when the object is completely destroyed
Call the counter retainCount = 0 when the object is destroyed
//dealloc cannot be called manually
// in dealloc, clear member variables, proxies, listeners, etc
Self. Color = nil;
Self. LinePoints = nil;
[super dealloc];
}





Constructors and destructors for the Objective-c class


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.