Objective-c:arc automatically frees object memory

Source: Internet
Author: User



ARC is a reference counting mechanism that cocoa system helps you to complete object memory release






. h file


 
1 //  Person.h
 2 //  01-ARC
 3 //
 4 //  Created by ma c on 15/8/13.
 5 //  Copyright (c) 2015年 bjsxt. All rights reserved.
 6 //
 7 
 8 #import <Foundation/Foundation.h>
 9 
10 @interface Person : NSObject
11 @property(nonatomic,strong)NSString *name;
12 @property(nonatomic,assign)NSInteger age;
13 +(Person*)personWithName:(NSString*) name andAge:(NSInteger) age;
14 -(id)initWithName:(NSString*) name andAge:(NSInteger) age;
15 -(void)show;
16 @end





    . m file


 
1 // Person.m
 2 // 01-ARC
 3 //
 4 // Created by ma c on 15/8/13.
 5 // Copyright (c) 2015 bjsxt. All rights reserved.
 6 //
 7
 8 #import "Person.h"
 9 
10 @implementation Person
11-(id) initWithName: (NSString *) name andAge: (NSInteger) age
12 {
13 self = [super init];
14 if (self)
15 {
16 _name = name;
17 _age = age;
18}
19 return self;
20}
twenty one 
twenty two /*
23 In the class method, since no object instance is created, the self pointer cannot be used, and the instance variable cannot be used.
twenty four  */
25 + (Person *) personWithName: (NSString *) name andAge: (NSInteger) age
26 {
27 return [[Person alloc] initWithName: name andAge: age];
28}
29
30-(void) show
31 {
32 NSLog (@ "name:% @, age:% ld", _ name, _age);
33}
34
35 / *
36 When creating an object, first create the part of the parent class, and then create the part of the subclass;
37 When destroying objects, the order is reversed
38 ARC forbids sending explicit dealloc messages
39 * /
40-(void) dealloc
41 {
42 NSLog (@ "person dealloc");
43 // [super dealloc]; // Forbid sending explicit dealloc messages explicitly
44}
45 @end





    Main function test


1 // main.m
  2 // 01-ARC
  3 //
  4 // Created by ma c on 15/8/13.
  5 // Copyright (c) 2015 bjsxt. All rights reserved.
  6 //
  7
  8 #import <Foundation / Foundation.h>
  9 #import "Person.h"
10 int main (int argc, const char * argv [])
11 {
12 @autoreleasepool
13 {
14 Person * person = [[Person alloc] initWithName: @ "Jim" andAge: 22];
15
16 [person show];
17 // [person dealloc]; // error, the bottom layer will automatically call this method to destroy the object
18}
19 return 0;
20} 





Test results:


2015-08-13 17:48:54.904 01-ARC[1636:107161] name:Jim,age:22
2015-08-13 17:48:54.905 01-ARC[1636:107161] person dealloc
Program ended with exit code: 0


 





Objective-c:arc automatically frees object memory


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.