ios 使用宏创建标准单利
2015-10-22 14:04
645 查看
// // Singleton2.h // 单例的标准写法 // 来自 轩哥 #import <Foundation/Foundation.h> //使用宏实现单例模板 // 在宏里 \ 表示连接符 宏定义 只能一行 @interface Singleton2 : NSObject #define DEFINE_SINGLETON_FOR_HEADER(className) \ \ + (className *)shared##className; //## 在宏定义表示 连接 宏参数 DEFINE_SINGLETON_FOR_HEADER(Singleton2) @end
// // Singleton2.m // 单例的标准写法 //<pre name="code" class="objc">// 来自 轩哥
// // Singleton2.m // 单例的标准写法 // // Created by LZXuan on 14-8-24. // Copyright (c) 2014年 LZXuan. All rights reserved. // #import "Singleton2.h" @implementation Singleton2 #define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \ \ static classname *shared##classname = nil; \ \ + (classname *)shared##classname \ { \ @synchronized(self) \ { \ if (shared##classname == nil) \ { \ shared##classname = [[self alloc] init]; \ } \ } \ \ return shared##classname; \ } \ \ + (id)allocWithZone:(NSZone *)zone \ { \ @synchronized(self) \ { \ if (shared##classname == nil) \ { \ shared##classname = [super allocWithZone:zone]; \ return shared##classname; \ } \ \ } \ \ return nil; \ } \ \ - (id)copyWithZone:(NSZone *)zone \ { \ return self; \ } \ \ - (id)retain \ { \ return self; \ } \ \ - (NSUInteger)retainCount \ { \ return UINT_MAX; \ } \ \ - (oneway void)release \ { \ } \ \ - (id)autorelease \ { \ return self; \ } SYNTHESIZE_SINGLETON_FOR_CLASS(Singleton2) @end
相关文章推荐
- iOS 文件下载
- ios显示gif格式图片
- iOS 2D绘图详解(Quartz 2D)之路径(点,直线,虚线,曲线,圆弧,椭圆,矩形)
- iOS获取User-Agent(UA)信息的方法
- iOS调试程序时,启动应用失败的解决办法
- 【iOS】【实例】自定义一个简单的TabBar
- iOS获取当前连接的WiFi名称即SSID的方法
- iOS谓词使用--NSPredicate
- IOS 委托和协议区别和联系 (=)
- IOS --委托和协议区别和联系(一)
- iOS中button选择其中一个角改变弧度(适用其他控件)
- iOS property属性
- iOS获取手机型号
- ios -- ViewController跳转+传值(升级版)
- 工作备忘-ios应用安全防护oc源代码混淆,防止class-dump攻击
- 整理iOS9适配的一些坑
- IOS—使用Storyboard实现复杂界面
- ios学习规划
- iOS - 定位服务
- iOS如何把所有界面的状态栏的字体颜色都设置为白色