Objective-C:MRC手动释放对象内存举例(引用计数器)
手机内存下的类的设计练习:
设计Book类,
1.三个成员变量:
title(书名)author(作者)、price(价格)
2.不使用@property,自己完成存取方法(set方法,get方法)
3、加入必要其他的方法
4、并对Book类进行测试
.h声明文件
1 // Book.h 2 // 引用计数器 3 // 4 // Created by ma c on 15/8/13. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 10 @interface Book : NSObject 11 { 12 NSString *_title; 13 NSString *_author; 14 CGFloat _price; 15 } 16 -(id)initWithTitle:(NSString*)title andAuthor:(NSString*)author 17 AndPrice:(CGFloat)price; 18 -(void)setTitle:(NSString*) title; 19 -(void)setAuthor:(NSString*) author; 20 -(void)setPrice:(CGFloat) price; 21 -(NSString*) title; 22 -(NSString*) author; 23 -(CGFloat) price; 24 -(void) show; 25 @end
.m声明文件
1 // Book.m 2 // 引用计数器 3 // 4 // Created by ma c on 15/8/13. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import "Book.h" 9 10 @implementation Book 11 -(id)initWithTitle:(NSString*)title andAuthor:(NSString*)author 12 AndPrice:(CGFloat)price 13 { 14 self = [super init]; 15 if(self) 16 { 17 _title = [title retain]; 18 _author = [author retain]; 19 _price = price; 20 } 21 return self; 22 } 23 -(void)setTitle:(NSString*) title 24 { 25 if(_title != title) 26 { 27 [_title release];//释放上一次拥有的对象所有权 28 _title = [title retain];//获取这一次的对象所有权 29 } 30 } 31 -(void)setAuthor:(NSString*) author 32 { 33 if(_author != author) 34 { 35 [_author release];//释放上一次拥有的对象所有权 36 _author = [author retain];//获取这一次的对象所有权 37 } 38 } 39 -(void)setPrice:(CGFloat) price 40 { 41 _price = price; 42 } 43 -(NSString*) title 44 { 45 return _title; 46 } 47 -(NSString*) author 48 { 49 return _author; 50 } 51 -(CGFloat) price 52 { 53 return _price; 54 } 55 -(void) show 56 { 57 NSLog(@"title:%@,author:%@,price:%.2f",_title,_author,_price); 58 } 59 -(void)dealloc 60 { 61 [_title release]; 62 [_author release]; 63 NSLog(@"title retainCount:0"); 64 NSLog(@"author retainCount:0"); 65 NSLog(@"book retainCount:0"); 66 NSLog(@"book is dealloc!"); 67 [super dealloc]; 68 } 69 @end
测试Book类
1 // main.m 2 // 引用计数器 3 // 4 // Created by ma c on 15/8/13. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 #import "Book.h" 10 int main(int argc, const char * argv[]) 11 { 12 //@autoreleasepool { 13 14 //创建书对象book并初始化 15 Book *book = [[Book alloc]initWithTitle:@"OC" andAuthor:@"Jobs" AndPrice:35.6];//book count:1 16 NSLog(@"book retainCount:%lu",[book retainCount]); 17 18 //创建书名对象title 19 NSMutableString *title = [NSMutableString stringWithString:@"IOS"];//title count:1 20 NSLog(@"title retainCount:%lu",[title retainCount]); 21 22 //设置书名 23 [book setTitle: title];//title count:2 24 NSLog(@"title retainCount:%lu",[title retainCount]); 25 26 //创建书的作者对象author 27 NSMutableString *author = [NSMutableString stringWithString:@"Bill"];//author count:1 28 NSLog(@"author retainCount:%lu",[author retainCount]); 29 30 //设置书的作者名 31 [book setAuthor:author];//author count:2 32 NSLog(@"author retainCount:%lu",[author retainCount]); 33 34 35 //设置书的价格 36 [book setPrice:58.9]; 37 38 39 //释放title对象所有权----与上面的创建title对象相对应 40 [title release];//title count:1 41 NSLog(@"title retainCount:%lu",[title retainCount]); 42 43 //释放author对象所有权----与上面的创建author对象相对应 44 [author release];//author count:1 45 NSLog(@"author retainCount:%lu",[author retainCount]); 46 47 48 //释放在book类中的成员实例变量title和author对象的所有权,并销毁book对象 49 [book show]; 50 [book release];//title count:0, author count:0 ,book count:0, dealloc book 51 //} 52 return 0; 53 }
运行结果:
2015-08-13 16:56:49.608 引用计数器[1527:94167] book retainCount:1 2015-08-13 16:56:49.609 引用计数器[1527:94167] title retainCount:1 2015-08-13 16:56:49.610 引用计数器[1527:94167] title retainCount:2 2015-08-13 16:56:49.610 引用计数器[1527:94167] author retainCount:1 2015-08-13 16:56:49.610 引用计数器[1527:94167] author retainCount:2 2015-08-13 16:56:49.610 引用计数器[1527:94167] title retainCount:1 2015-08-13 16:56:49.610 引用计数器[1527:94167] author retainCount:1 2015-08-13 16:56:49.610 引用计数器[1527:94167] title:IOS,author:Bill,price:58.90 2015-08-13 16:56:49.611 引用计数器[1527:94167] title retainCount:0 2015-08-13 16:56:49.611 引用计数器[1527:94167] author retainCount:0 2015-08-13 16:56:49.611 引用计数器[1527:94167] book retainCount:0 2015-08-13 16:56:49.611 引用计数器[1527:94167] book is dealloc! Program ended with exit code: 0
可以看出:
计数器:retainCount
对象中存储被引用的次数,
当被引用的时候,计数器加1;
不在引用的时候,计数器减1;
当计数器为0的时候,真正去销毁对象。
程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!