OBJECTIVE-C Learning Notes--constructors and destructors

Source: Internet
Author: User



In a nutshell, a constructor is a function of initializing an object when it is created, initializing some of the member variables in the object, and the destructor is doing some cleanup work on the object being deleted, that is, the specialized wrap-up work, which is described in code below.






First, the constructor function



  In OC, all functions that begin with INIT are referred to as constructors, and when declaring constructors, the general declaration without parameters is "-(ID) init", and the general statement with parameters is "-(ID) initwith ...".


1 @interface Person: NSObject {
  2 @private
  3 int age;
  4 NSString * name;
  5}
  6-(id) init; // Constructor without parameters
  7-(id) initWithname: (NSString *) newname age: (int) newage; // Constructor with parameters
  8 
  9 @end
10
11
12 @implementation Person
13
14-(id) init {
15 self = [super init];
16 if (self) {
17 name = @ “xiaoming”;
18 age = 20;
19}
20 return self;
twenty one }
twenty two 
23-(id) initWithname: (NSString *) newname age: (int) newage {
24 if (self = [super init]) {
25 age = newage;
26 name = newname;
27}
28 return self;
29}
30 int main (int argc, const char * argv []) {
31
32 Person * p1 = [[Person alloc] init]; // Call the initialization function after the object is generated
33 Person * P2 = [[Person alloc] initWithnam: (NSString *) xiaohong age: 19];
34 // Call the constructor with parameters for initialization
35 return 0;
36} 


This code, declared in the person class, two initialization functions, the first with the second parameter without parameters, the initialization of the image when the initialization function is more flexible, the initial value can be changed.






Second, the destructor function



The destructor is declared as "-(void) Dealloc" and we cannot invoke it by object, and the destructor will run on its own as soon as it is dying, to see the code.


1 @interface Person: NSObject {
  2 @private
  3 int age;
  4 NSString * name;
  5}
  6-(void) dealloc; // Declaration destructor, there can only be one destructor
  7 @end
  8 
  9 
10 @implementation Person // Implement the Person class
11-(void) dealloc {
12 NSLog (@ "this is dealloc function");
13 [super dealloc];
14}
15
16 @end
17
18 Person * xiaoming = [Person alloc];
19 [xiaoming release]; 


In line sixth of the code we declare the destructor and we make a copy of it at line 11th, which runs when [xiaoming release] is executed, because the release function destroys the object.






Note: Sometimes we add [super Dealloc] when we make a dealloc function; An error occurs.



This is because ARC is the new feature introduced by iOS 5, called arc (Automatic referencecounting). Simply put, it is the code that automatically joins the Retain/release, and the code that originally needed to manually add a reference count to handle memory management can be automatically completed by the compiler. This mechanism is started in iOS 5/mac OS X 10.7 and can be used with Xcode4.2. The simple understanding of arc is that by specifying the syntax, the compiler (LLVM3.0) automatically generates the reference count of the instance when the code is compiled to manage part of the code. One thing, ARC is not a GC, it's just a code static analysis (Staticanalyzer) tool.



WORKAROUND: Double-hit the project name in between, enter build setting





Change the middle objective-c Automatic Reference counting to no OK!















OBJECTIVE-C Learning Notes--constructors and destructors


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.