BogoSingleton.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // .h文件
  2. #define BogoSingletonH(name) + (instancetype)shared##name;
  3. // .m文件
  4. #if __has_feature(objc_arc)
  5. #define BogoSingletonM(name) \
  6. static id _instace; \
  7. \
  8. + (id)allocWithZone:(struct _NSZone *)zone \
  9. { \
  10. static dispatch_once_t onceToken; \
  11. dispatch_once(&onceToken, ^{ \
  12. _instace = [super allocWithZone:zone]; \
  13. }); \
  14. return _instace; \
  15. } \
  16. \
  17. + (instancetype)shared##name \
  18. { \
  19. static dispatch_once_t onceToken; \
  20. dispatch_once(&onceToken, ^{ \
  21. _instace = [[self alloc] init]; \
  22. }); \
  23. return _instace; \
  24. } \
  25. \
  26. - (id)copyWithZone:(NSZone *)zone \
  27. { \
  28. return _instace; \
  29. }
  30. #else
  31. #define BogoSingletonM(name) \
  32. static id _instace; \
  33. \
  34. + (id)allocWithZone:(struct _NSZone *)zone \
  35. { \
  36. static dispatch_once_t onceToken; \
  37. dispatch_once(&onceToken, ^{ \
  38. _instace = [super allocWithZone:zone]; \
  39. }); \
  40. return _instace; \
  41. } \
  42. \
  43. + (instancetype)shared##name \
  44. { \
  45. static dispatch_once_t onceToken; \
  46. dispatch_once(&onceToken, ^{ \
  47. _instace = [[self alloc] init]; \
  48. }); \
  49. return _instace; \
  50. } \
  51. \
  52. - (id)copyWithZone:(NSZone *)zone \
  53. { \
  54. return _instace; \
  55. } \
  56. \
  57. - (oneway void)release { } \
  58. - (id)retain { return self; } \
  59. - (NSUInteger)retainCount { return 1;} \
  60. - (id)autorelease { return self;}
  61. #endif