How to properly implement ARC compatible and `alloc init` safe Singleton class?
来源:互联网 发布:遨龙一号知乎 编辑:程序博客网 时间:2024/05/21 06:28
Apple recommends the strict singleton implementation (no other living object of the same type is allowed) this way:
+ (instancetype)singleton { static id singletonInstance = nil; if (!singletonInstance) { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ singletonInstance = [[super allocWithZone:NULL] init]; }); } return singletonInstance;}+ (id)allocWithZone:(NSZone *)zone { return [self singleton];}- (id)copyWithZone:(NSZone *)zone { return self;}
摘录:http://stackoverflow.com/questions/17809290/how-to-properly-implement-arc-compatible-and-alloc-init-safe-singleton-class
0 0
- How to properly implement ARC compatible and `alloc init` safe Singleton class?
- How to implement a singleton class in Symbian OS
- How to write a thread-safe Singleton?
- [Ruby]How to create singleton class ?
- Interview Series: How To Implement Singleton In Concurrent Environment?
- How to define a template class in a .h file and implement it in a .cpp file
- Why to use memory pool and how to implement it
- How to close resources properly
- How to properly unblock libusb_bulk_transfer
- How to implement fiber
- how to implement QAbstractScrollArea
- How to properly convert SecureString to String
- How to create singleton
- How RGB is related to HSV, and how to implement hue shifting on the iPhone
- Why Prepared Statements are important and how to use them "properly"
- understanding positions and screen resolution, how to properly set position of object?
- how to display chinese character properly
- How to set Android camera orientation properly?
- 如何写一篇好的技术博客
- 应该知道的Linux技巧
- 联想A766开发的应用无法退出的问题
- java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not
- java.lang.UnsupportedClassVersionError: Bad version number in .class file
- How to properly implement ARC compatible and `alloc init` safe Singleton class?
- java 加密解密简单实现
- 一些简单的编程练习题
- 为地球朝着什么方翻译成英文"朝
- php模拟post提交数据
- Python编程练习——初级篇
- JUnit基础及第一个单元测试实例(JUnit3.8)
- 小型项目的用户角色权限的设计
- java实现两个数相除并保留指定有效数字