Guest User

Untitled

a guest
Aug 17th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. //
  2. // GCDSingleton.h
  3. //
  4. // Macros to simplify creating of a sharedInstance for a class using GCD (ARC compliant)
  5. //
  6. // Use one of the following methods to create a sharedInstance
  7. //
  8.  
  9. //
  10. // METHOD #1: use this if you want the sharedInstance method to be 'sharedClassName'
  11. //
  12. // @implementation MySharedThing
  13. //
  14. // DEFINE_SHARED_INSTANCE_FOR_CLASS(MySharedThing)
  15. //
  16.  
  17. #define DEFINE_SHARED_INSTANCE_FOR_CLASS(className) \
  18. + (id)shared##className \
  19. { \
  20. static dispatch_once_t pred = 0; \
  21. __strong static id _sharedObject = nil; \
  22. dispatch_once(&pred, ^{ \
  23. _sharedObject = ^{return [[self alloc] init];}(); \
  24. }); \
  25. return _sharedObject; \
  26. } \
  27.  
  28.  
  29. //
  30. // METHOD #2: use this if you want to name the 'sharedXXX' method yourself
  31. //
  32. // @implementation MySharedThing
  33. //
  34. // + (id)sharedInstance
  35. // {
  36. // DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
  37. // return [[self alloc] init];
  38. // });
  39. // }
  40. //
  41. // @end
  42. //
  43.  
  44. #define DEFINE_SHARED_INSTANCE_USING_BLOCK(block) \
  45. static dispatch_once_t pred = 0; \
  46. __strong static id _sharedObject = nil; \
  47. dispatch_once(&pred, ^{ \
  48. _sharedObject = block(); \
  49. }); \
  50. return _sharedObject; \
Add Comment
Please, Sign In to add comment