Objective-C:禁止调用方法
2015-10-08 11:02
337 查看
通过unavailable宏来禁止调用某些属性方法
通过unavailable宏来禁止调用某些属性方法
#import <Foundation/Foundation.h> @interface MySingleton : NSObject +(instancetype) sharedInstance; // 外部调用将产生编译错误 +(instancetype) alloc __attribute__((unavailable("alloc not available, call sharedInstance instead"))); -(instancetype) init __attribute__((unavailable("init not available, call sharedInstance instead"))); +(instancetype) new __attribute__((unavailable("new not available, call sharedInstance instead"))); @end #import "MySingleton.h"
//单例模式实现 @implementation MySingleton //外部只能通过调用这个静态方法获得唯一的实例 +(instancetype) sharedInstance { static dispatch_once_t pred; static id shared = nil; dispatch_once(&pred, ^{ shared = [[super alloc] initUniqueInstance]; }); return shared; } //生成唯一的实例 -(instancetype) initUniqueInstance { return [super init]; } @end
相关文章推荐
- Object.wait()与Object.notify()的用法
- 在Object-C中使用@property和@synthesize的区别
- java Object类学习
- Objective-C---9---内存管理
- Objective-C对象属性设置
- Lost in quantization: improving particular object retrieval in large scale image databases
- iOS开发之Objective-C与JavaScript的交互
- iOS开发之Objective-C与JavaScript的交互
- Objective-C Class Ivar Layout 探索
- 2015 Objective-C 三大新特性
- 2015 Objective-C 三大新特性
- Objective-C中的instancetype和id…
- Objective-C中的instancetype和id…
- Object-c学习笔记十八-----NSPredicate
- iOS开发 —— Objective C语言知识点总结(2)
- json字符串转换为JSONObject和JSONArray
- 数据存取的方式和应用沙盒目录的常见获取方式
- 【OC基础】基础笔记01
- Any、AnyObject与is、as
- java bind object-c by using bro-gen