. h declaration File
1 // Integer.h
2 // 02-MRC
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 Integer : NSObject
11 @property(nonatomic,assign)NSInteger i;
12 -(id)initWithI:(NSInteger) i;
13 -(void) print;
14 +(Integer *)integerWithIntger:(NSInteger) i;
15 @end
. m implementation file
1 // Integer.m
2 // 02-MRC
3 //
4 // Created by ma c on 15/8/13.
5 // Copyright (c) 2015年 bjsxt. All rights reserved.
6 //
7
8 #import "Integer.h"
9
10 @implementation Integer
11 -(id)initWithI:(NSInteger) i
12 {
13 self = [super init];
14 if(self)
15 {
16 _i = i;
17 }
18 return self;
19 }
20 +(Integer *)integerWithIntger:(NSInteger) i
21 {
22 return [[Integer alloc]initWithI:i];
23 }
24
25 -(void) print
26 {
27 NSLog(@"i = %ld",_i);
28 }
29 -(void)dealloc
30 {
31 NSLog(@"integer dealloc");
32 [super dealloc];
33 }
34 @end
Main function test
1 // main.m
2 // 02-MRC
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 "Integer.h"
10 int main (int argc, const char * argv [])
11 {
12 @autoreleasepool
13 {
14 // Test manual reference count
15 // 1. Create object will get object ownership
16 Integer * i1 = [[Integer alloc] initWithI: 10];
17 NSLog (@ "retaincount =% lu", [i1 retainCount]); // 1
18
19
20 // 2. Assignment by pointer only, will not obtain object ownership
21 Integer * i2 = i1;
22 NSLog (@ "retaincount =% lu", [i2 retainCount]); // 1
twenty three
twenty four
25 // 3. Obtain ownership of the object through retain
26 [i1 retain];
27 NSLog (@ "retaincount =% lu", [i1 retainCount]); // 2
28
29
30 // 4. Add the object to the container, a reference to the object will be stored in the container, and object ownership will be obtained
31 NSMutableArray * array = [NSMutableArray array];
32 [array addObject: i1];
33 NSLog (@ "retaincount =% lu", [i1 retainCount]); // 3
34
35
36 // 5. Release ownership of the object through release
37 [i1 release];
38 NSLog (@ "retaincount =% lu", [i1 retainCount]); // 2
39
40
41 // 6. Deleting an object from the container will also release object ownership
42 [array removeObject: i1];
43 NSLog (@ "retaincount =% lu", [i1 retainCount]); // 1
44
45 // 7. Finally release it again, the object will be destroyed normally
46 [i1 release]; // At this time, the bottom layer will call the dealloc method // 0
47}
48 return 0;
49}
the test results are:
2015-08-13 17:32:36.408 02-MRC[1599:103515] retaincount = 1
2015-08-13 17:32:36.409 02-MRC[1599:103515] retaincount = 1
2015-08-13 17:32:36.410 02-MRC[1599:103515] retaincount = 2
2015-08-13 17:32:36.410 02-MRC[1599:103515] retaincount = 3
2015-08-13 17:32:36.410 02-MRC[1599:103515] retaincount = 2
2015-08-13 17:32:36.410 02-MRC[1599:103515] retaincount = 1
2015-08-13 17:32:36.410 02-MRC[1599:103515] integer dealloc
Program ended with exit code: 0
OBJECTIVE-C:MRC (reference counter) how to get ownership of objects (INIT, retain, copy, etc.)