Posted By

paki on 05/13/11


Tagged

Singleton objectivec


Versions (?)

Singleton Class


 / Published in: Objective C
 

  1. #import "SomeClass.h"
  2.  
  3. static SomeClass *instance = nil;
  4.  
  5. @implementation SomeClass
  6.  
  7. #pragma mark -
  8. #pragma mark Singleton
  9.  
  10. + (id)instance
  11. {
  12. @synchronized(self)
  13. {
  14. if (instance == nil)
  15. instance = [[super allocWithZone:NULL] init];
  16. }
  17. return instance;
  18. }
  19.  
  20. + (id)allocWithZone:(NSZone *)zone
  21. {
  22. return [[self instance] retain];
  23. }
  24.  
  25. - (id)copyWithZone:(NSZone *)zone
  26. {
  27. return self;
  28. }
  29.  
  30. - (id)retain
  31. {
  32. return self;
  33. }
  34.  
  35. - (unsigned)retainCount {
  36. return UINT_MAX;
  37. }
  38.  
  39. - (void)release
  40. {
  41. // never release
  42. }
  43.  
  44. - (id)autorelease
  45. {
  46. return self;
  47. }
  48.  
  49. - (id)init
  50. {
  51. if (self = [super init])
  52. {
  53. }
  54. return self;
  55. }
  56.  
  57. @end

Report this snippet  

You need to login to post a comment.