[关闭]
@SanMao 2015-08-07T09:56:18.000000Z 字数 1415 阅读 991

单例

知识补充


  1. //
  2. // Single.h
  3. // 单例
  4. //
  5. // Created by 沈方武 on 15/8/7.
  6. // Copyright © 2015年 沈方武. All rights reserved.
  7. // 快速创建单例
  8. #define interfaceSingle(name) + (instancetype)share##name
  9. #if __has_feature(objc_arc)
  10. // 如果是ARC
  11. #define implementationSingle(name) + (instancetype)share##name \
  12. { \
  13. return [[self alloc] init]; \
  14. } \
  15. static id _instance; \
  16. + (instancetype)allocWithZone:(struct _NSZone *)zone \
  17. { \
  18. static dispatch_once_t onceToken; \
  19. dispatch_once(&onceToken, ^{ \
  20. _instance = [super allocWithZone:zone]; \
  21. }); \
  22. return _instance; \
  23. } \
  24. - (id)copyWithZone:(NSZone *)zone \
  25. { \
  26. return _instance; \
  27. } \
  28. - (id)mutableCopyWithZone:(NSZone *)zone \
  29. { \
  30. return _instance; \
  31. }
  32. #else
  33. // 如果不是ARC
  34. #define implementationSingle(name) + (instancetype)share##name \
  35. { \
  36. return [[self alloc] init]; \
  37. } \
  38. static id _instance; \
  39. + (instancetype)allocWithZone:(struct _NSZone *)zone \
  40. { \
  41. static dispatch_once_t onceToken; \
  42. dispatch_once(&onceToken, ^{ \
  43. _instance = [super allocWithZone:zone]; \
  44. }); \
  45. return _instance; \
  46. } \
  47. - (id)copyWithZone:(NSZone *)zone \
  48. { \
  49. return _instance; \
  50. } \
  51. - (id)mutableCopyWithZone:(NSZone *)zone \
  52. { \
  53. return _instance; \
  54. }\
  55. - (oneway void)release \
  56. {} \
  57. - (instancetype)retain \
  58. { \
  59. return _instance; \
  60. } \
  61. - (NSUInteger)retainCount \
  62. { \
  63. return MAXFLOAT; \
  64. }
  65. #endif
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注