IOS--UI--单例
来源:互联网 发布:域名询价 编辑:程序博客网 时间:2024/05/17 22:11
单例的目的:永远只分配一个内存给创建对象
第一种写法
.h//1.创建代理对象+(ContactHelper *)shareContactHelper;.mstatic ContactHelper *helper =nil;//创建单例的方法+(ContactHelper *)shareContactHelper{ //安全机制 保护在多线程中访问的安全 @synchronized (self){ if (helper ==nil) { helper =[[ContactHelper alloc]init]; //单例对象不能释放 为了保证任何对象都被访问 由于他的生命周期和程序一样 所有他的空间在程序的整个运行过程中都不会被收回(因为存放在静态区啊) [helper readDataFromPlist]; } }return helper;}
第二种方法:与线程结合
①ARC
.m//定义一份变量(整个运行过程中,只有一份)static id _instace = nil;+(instancetype)shareAudioTool{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instace = [[super alloc]init]; }); return _instace;}//重写 alloc 方法 :目的控制内存分配 永远只分配一次内存+(id)allocWithZone:(struct _NSZone *)zone{ //里面代码永远执行一次 static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instace = [super allocWithZone:zone]; });// 返回对象 return _instace;}//初始化就一次-(id)init{ if (self =[super init]) { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ //加载做需要的音频资源 }); }return self;}//这个方法是 让所有的 alloc init 都只执行一次 用于 ARC 状态下
② MRC
考虑到 release 的问题
.mstatic id _instance; \+ (id)allocWithZone:(struct _NSZone *)zone \{ \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instance = [super allocWithZone:zone]; \ }); \ return _instance; \} \ \+ (instancetype)shared##name \{ \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instance = [[self alloc] init]; \ }); \ return _instance; \} \ \- (oneway void)release \{ \ \} \ \- (id)autorelease \{ \ return _instance; \} \ \- (id)retain \{ \ return _instance; \} \ \- (NSUInteger)retainCount \{ \ return 1; \} \ \+ (id)copyWithZone:(struct _NSZone *)zone \{ \ return _instance; \}
代码因为这些太长 不方便我们使用 就写成宏 可是宏只认同一行所以在每一行后面加\ 告诉系统 后面的也是他的
注:最后一行就不用了 因为你后面没有了
下面 是完整的非 ARC 的单例宏
// ## : 连接字符串和参数#define singleton_h(name) + (instancetype)shared##name;#define singleton_m(name) \static id _instance; \+ (id)allocWithZone:(struct _NSZone *)zone \{ \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instance = [super allocWithZone:zone]; \ }); \ return _instance; \} \ \+ (instancetype)shared##name \{ \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instance = [[self alloc] init]; \ }); \ return _instance; \} \ \- (oneway void)release \{ \ \} \ \- (id)autorelease \{ \ return _instance; \} \ \- (id)retain \{ \ return _instance; \} \ \- (NSUInteger)retainCount \{ \ return 1; \} \ \+ (id)copyWithZone:(struct _NSZone *)zone \{ \ return _instance; \}
在你的工具类里只要 引入这个头文件
写两行代码就可以完成单例
.h#import <Foundation/Foundation.h>#import "Singleton.h"@interface HMNetTool : NSObjectsingleton_h(NetTool)@end.m#import "HMNetTool.h"@implementation HMNetTool- (id)init{ if (self = [super init]) { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ // 加载资源 }); } return self;}singleton_m(NetTool)@end
总:两种方法都可以使用
0 0
- IOS--UI--单例
- iOS UI 11 单例
- iOS UI 21 单例中的线程
- IOS单视图UI笔记
- ios-UI高级多线程 GCD实现单例模式
- iOS UI篇 - @synchronized与dispatch_once 创建单例
- UI 18 单例
- ios开发UI单视图控制器
- ios开发UI单视图控制器 [下]
- UI之单例模式
- UI高级-UIApplication单例
- 单例模式:单例类的标准写法 - iOS - UI基础知识总结13
- iOS UI界面之间传值方式的整理总结(2)单例方法
- iOS开发系列----UI(视图编程入门:Delegate、Block、单例、属性传值)
- UI基础-iOS黑马-UITableView2-单组数据展示 LOL
- iOS单例模式
- iOS 单例模式
- IOS 单例模式
- js详细基本总结
- iOS每日一记之---------改变Btn点击时的背景颜色
- Hibernate学习笔记(七) — 查询缓存的应用
- gunplot画柱形图(windows下)
- 部署服务的时候报错 ArgumentError: invalid byte sequence in US-ASCII
- IOS--UI--单例
- tableview click
- 交换字符串
- 计算机系统 (五)存储设备层次结构
- Android Layout_weight属性完全解析以及使用ListView来实现表格
- JavaScript 32位整型无符号操作
- MFC实现256色工具栏图标及工具栏提示信息
- 高斯函数及其图像滤波中的运用
- PHP Client for Mysql Binlog