Objective-C:MRC手动释放对象内存举例(引用计数器)
2024-10-16 01:45:59
手机内存下的类的设计练习:
设计Book类,
1.三个成员变量:
title(书名)author(作者)、price(价格)
2.不使用@property,自己完成存取方法(set方法,get方法)
3、加入必要其他的方法
4、并对Book类进行测试
.h声明文件
// Book.h
// 引用计数器
//
// Created by ma c on 15/8/13.
// Copyright (c) 2015年. All rights reserved.
// #import <Foundation/Foundation.h> @interface Book : NSObject
{
NSString *_title;
NSString *_author;
CGFloat _price;
}
-(id)initWithTitle:(NSString*)title andAuthor:(NSString*)author
AndPrice:(CGFloat)price;
-(void)setTitle:(NSString*) title;
-(void)setAuthor:(NSString*) author;
-(void)setPrice:(CGFloat) price;
-(NSString*) title;
-(NSString*) author;
-(CGFloat) price;
-(void) show;
@end
.m声明文件
// Book.m
// 引用计数器
//
// Created by ma c on 15/8/13.
// Copyright (c) 2015年. All rights reserved.
// #import "Book.h" @implementation Book
-(id)initWithTitle:(NSString*)title andAuthor:(NSString*)author
AndPrice:(CGFloat)price
{
self = [super init];
if(self)
{
_title = [title retain];
_author = [author retain];
_price = price;
}
return self;
}
-(void)setTitle:(NSString*) title
{
if(_title != title)
{
[_title release];//释放上一次拥有的对象所有权
_title = [title retain];//获取这一次的对象所有权
}
}
-(void)setAuthor:(NSString*) author
{
if(_author != author)
{
[_author release];//释放上一次拥有的对象所有权
_author = [author retain];//获取这一次的对象所有权
}
}
-(void)setPrice:(CGFloat) price
{
_price = price;
}
-(NSString*) title
{
return _title;
}
-(NSString*) author
{
return _author;
}
-(CGFloat) price
{
return _price;
}
-(void) show
{
NSLog(@"title:%@,author:%@,price:%.2f",_title,_author,_price);
}
-(void)dealloc
{
[_title release];
[_author release];
NSLog(@"title retainCount:0");
NSLog(@"author retainCount:0");
NSLog(@"book retainCount:0");
NSLog(@"book is dealloc!");
[super dealloc];
}
@end
测试Book类
// main.m
// 引用计数器
//
// Created by ma c on 15/8/13.
// Copyright (c) 2015年. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Book.h"
int main(int argc, const char * argv[])
{
//@autoreleasepool { //创建书对象book并初始化
Book *book = [[Book alloc]initWithTitle:@"OC" andAuthor:@"Jobs" AndPrice:35.6];//book count:1
NSLog(@"book retainCount:%lu",[book retainCount]); //创建书名对象title
NSMutableString *title = [NSMutableString stringWithString:@"IOS"];//title count:1
NSLog(@"title retainCount:%lu",[title retainCount]); //设置书名
[book setTitle: title];//title count:2
NSLog(@"title retainCount:%lu",[title retainCount]); //创建书的作者对象author
NSMutableString *author = [NSMutableString stringWithString:@"Bill"];//author count:1
NSLog(@"author retainCount:%lu",[author retainCount]); //设置书的作者名
[book setAuthor:author];//author count:2
NSLog(@"author retainCount:%lu",[author retainCount]); //设置书的价格
[book setPrice:58.9]; //释放title对象所有权----与上面的创建title对象相对应
[title release];//title count:1
NSLog(@"title retainCount:%lu",[title retainCount]); //释放author对象所有权----与上面的创建author对象相对应
[author release];//author count:1
NSLog(@"author retainCount:%lu",[author retainCount]); //释放在book类中的成员实例变量title和author对象的所有权,并销毁book对象
[book show];
[book release];//title count:0, author count:0 ,book count:0, dealloc book
//}
return ;
}
运行结果:
-- ::49.608 引用计数器[:] book retainCount:
-- ::49.609 引用计数器[:] title retainCount:
-- ::49.610 引用计数器[:] title retainCount:
-- ::49.610 引用计数器[:] author retainCount:
-- ::49.610 引用计数器[:] author retainCount:
-- ::49.610 引用计数器[:] title retainCount:
-- ::49.610 引用计数器[:] author retainCount:
-- ::49.610 引用计数器[:] title:IOS,author:Bill,price:58.90
-- ::49.611 引用计数器[:] title retainCount:
-- ::49.611 引用计数器[:] author retainCount:
-- ::49.611 引用计数器[:] book retainCount:
-- ::49.611 引用计数器[:] book is dealloc!
Program ended with exit code:
可以看出:
计数器:retainCount
对象中存储被引用的次数,
当被引用的时候,计数器加1;
不在引用的时候,计数器减1;
当计数器为0的时候,真正去销毁对象。
最新文章
- javascript学习之通过class获取元素
- Stanford机器学习---第二讲. 多变量线性回归 Linear Regression with multiple variable
- eclipse中文乱码
- 生成.a文件步骤
- android开发 drawtext的开始坐标位置
- Centos6 httpd与tomcat整合发布
- JSON互转
- C++_转换转子(4种)
- thinkphp3.2.x多图上传并且生成多张缩略图
- C#串口发送数据
- 安卓学习笔记--已root设备应用请求root权限
- jquery动态设置图片路径和超链接href属性
- 将LibreOffice文档转换为豆瓣日记
- fillder---断言,更改提交数据
- Dictionary集合运用
- SqlSugar ORM 的学习
- Hadoop原生态版安装
- python中安装并使用redis
- mysql 的sleep线程过多处理方法
- PX Deq: Execution Msg 等待事件
热门文章
- ubuntu16.04 删除内核
- MongoDB图形化管理工具Toad Mac Edition
- IOS 本地推送
- 层级目录结构的Makefile递归编译方法
- 浅谈 Java 中的枚举
- 整理之DOM事件阶段、冒泡与捕获、事件委托、ie事件和dom模型事件、鼠标事件
- CSS HTML 常用属性备忘录
- 【CF148D】 Bag of mice (概率DP)
- 重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor
- 【BZOJ】4318: OSU!【期望DP】