[Good programmer training camp] Objective-C learning notes --- constructor and destructor, objective-c learning notes

Source: Internet
Author: User

[Good programmer training camp] Objective-C learning notes --- constructor and destructor, objective-c learning notes

<A href = "http://www.goodprogrammer.org/" target = "blank"> ios training </A> ------ my OC Language notes, I look forward to communicating with you!

 

In short, constructor initializes an object and initializes some member variables of the object, the Destructor cleans up objects after they are deleted. The code below is used to describe them.

 

1. Constructor

In OC, all functions starting with init are called constructor. When a constructor is declared, a function without parameters is generally declared as "-(id) init ", the general declaration with parameters is "-(id) initWith... ".

1 @ interface Person: NSObject {2 @ private 3 int age; 4 NSString * name; 5} 6-(id) init; // constructors without parameters 7-(id) initWithname :( NSString *) newname age :( int) newage; // constructor with parameters 8 9 @ end10 11 12 @ implementation Person13 14-(id) init {15 self = [super init]; 16 if (self) {17 name = @ "xiaoming"; 18 age = 20; 19} 20 return self; 21} 22 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 33 Person * P2 = [[Person alloc] initWithnam :( NSString *) xiaohong age: 19] After the object is generated; 34 // call the constructor with parameters to initialize 35 return 0; 36}

In this Code, it is declared in the Person class that two initialization functions, the first with the parameter and the second without the parameter, during the initialization of the object, the initialization function with parameters is more flexible, and the initial values can be changed by yourself.

 

Ii. destructor

The Destructor is declared as "-(void) dealloc". We cannot manually call this function through the object. The Destructor runs itself when the object is about to die, let's look at the code.

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

In the sixth line of the code, we declared the Destructor and rewrote it in row 11th. This function will be executed when [xiaoming release, because the release function destroys objects.

 

Note: Sometimes we add [super dealloc] When re-writing the dealloc function; an error occurs.

This is because ARC is a new feature launched by iOS 5, which is called Automatic ReferenceCounting ). Simply put, retain/release is automatically added to the Code. The Code that was manually added to handle the reference count of memory management can be automatically completed by the compiler. This mechanism starts to be imported in iOS 5/Mac OS X 10.7 and can be used in Xcode4.2. A simple understanding of ARC means that the compiler (LLVM3.0) will automatically generate the reference count of the instance to manage part of the code During code compilation through the specified syntax. One thing is that ARC is not a GC, but a code static analysis (StaticAnalyzer) tool.

Solution: double-click the project name in the middle to go to build setting.

Change the Objective-C Automatic Reference Counting in the middle to no!

 

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.