浅谈——用宏封装单例
2015-09-24 00:14
330 查看
//
// Single.h
// 单例模式
//
// Created by lisilong on 15/8/18.
// Copyright © 2015年 longshao. All rights reserved.
//
// 判断当前是否时ARC
// 注意点:
// 1.单例是不可以继承的,
如果继承引发问题
// * 如果先创建父类,
那么永远都是父类
// * 如果先创建子类,
那么永远都是子类
// 说明:
// 1.调用allocWithZone:方法,给对象分配内存空间;
// 2.retainCount方法返回-1,或无限大MAXFLOAT只是让别的程序员一块便知道你这是一个单利对象。
/** 类的声明部分 */
#define SingleInterface(name) + (instancetype)share##name
/** 类的实现部分 */
#if __has_feature(objc_arc)
// ARC
#define SingleImplement(name) + (instancetype)share##name \
{ \
return [[self alloc] init]; \
} \
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static id instance; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
instance = [super allocWithZone:zone]; \
}); \
return instance; \
} \
- (nonnull id)copyWithZone:(nullable NSZone *)zone \
{ \
return self; \
} \
- (id)mutableCopyWithZone:(nullable NSZone *)zone \
{ \
return self; \
}
#else
// MRC
#define SingleImplement(name) + (instancetype)share##name \
{ \
return [[self alloc] init]; \
} \
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static id instance; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
instance = [super allocWithZone:zone]; \
}); \
return instance; \
} \
- (nonnull id)copyWithZone:(nullable NSZone *)zone \
{ \
return self; \
} \
- (id)mutableCopyWithZone:(nullable NSZone *)zone \
{ \
return self; \
} \
- (oneway void)release \
{} \
- (instancetype)retain \
{ \
return self; \
} \
-(NSUInteger)retainCount \
{ \
return MAXFLOAT; \
}
#endif
// Single.h
// 单例模式
//
// Created by lisilong on 15/8/18.
// Copyright © 2015年 longshao. All rights reserved.
//
// 判断当前是否时ARC
// 注意点:
// 1.单例是不可以继承的,
如果继承引发问题
// * 如果先创建父类,
那么永远都是父类
// * 如果先创建子类,
那么永远都是子类
// 说明:
// 1.调用allocWithZone:方法,给对象分配内存空间;
// 2.retainCount方法返回-1,或无限大MAXFLOAT只是让别的程序员一块便知道你这是一个单利对象。
/** 类的声明部分 */
#define SingleInterface(name) + (instancetype)share##name
/** 类的实现部分 */
#if __has_feature(objc_arc)
// ARC
#define SingleImplement(name) + (instancetype)share##name \
{ \
return [[self alloc] init]; \
} \
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static id instance; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
instance = [super allocWithZone:zone]; \
}); \
return instance; \
} \
- (nonnull id)copyWithZone:(nullable NSZone *)zone \
{ \
return self; \
} \
- (id)mutableCopyWithZone:(nullable NSZone *)zone \
{ \
return self; \
}
#else
// MRC
#define SingleImplement(name) + (instancetype)share##name \
{ \
return [[self alloc] init]; \
} \
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static id instance; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
instance = [super allocWithZone:zone]; \
}); \
return instance; \
} \
- (nonnull id)copyWithZone:(nullable NSZone *)zone \
{ \
return self; \
} \
- (id)mutableCopyWithZone:(nullable NSZone *)zone \
{ \
return self; \
} \
- (oneway void)release \
{} \
- (instancetype)retain \
{ \
return self; \
} \
-(NSUInteger)retainCount \
{ \
return MAXFLOAT; \
}
#endif
相关文章推荐
- 编写struts.xml文件时提示帮助信息
- Linux补丁学习笔记
- java.sql.SQLException的常见原因
- Codeforces Round #321 (Div. 2) E Kefa and Watch (线段树维护Hash)
- POJ 1064 Cable master (二分查找_经典题!!!)
- 编写一个函数,从一个字符串中去除多余的空格。
- 一个PHP操作大变量的例子
- 题目:寻找缺失的数
- [最小割最大流]UVa1515 - Pool construction
- IOS6屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统)
- 浅谈——定位的使用
- 注解(Annotation,JDK5.0新特性)
- IIS6 ASP 页的执行造成响应缓冲区超过其配置限制的解决方法
- Java Spring MVC分层设计
- 浅谈——定位(CoreLocation)的简介
- JavaScript的DOM编程--06--两个实验
- Week2学习过程报告
- NSString 格式化前面带0的数字
- GDB下查看内存命令(x命令)
- Android--SwipeRefreshLayout下拉刷新Demo