Don't like ads? PRO users don't see any ads ;-)
Guest

sokie singleton OB C

By: a guest on May 2nd, 2012  |  syntax: Objective C  |  size: 0.73 KB  |  hits: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #import "Singleton.h"
  2.  
  3. @implementation Singleton
  4.  
  5. static Singleton *sharedSingleton =nil;
  6.  
  7. + (Singleton *) sharedSingleton
  8. {
  9.   if (sharedSingleton == nil)
  10.   {
  11.     sharedSingleton = [[super allocWithZone:NULL] init];
  12.   }
  13.   return sharedSingleton;
  14. }
  15.  
  16. + (id)allocWithZone:(NSZone *)zone
  17. {
  18.  @synchronized(self)
  19.  {
  20.    if (sharedSingleton == nil)
  21.    {
  22.      sharedSingleton = [super allocWithZone:zone];
  23.      return sharedSingleton;
  24.    }
  25.  }
  26.  return nil;
  27. }
  28.  
  29. - (id)copyWithZone:(NSZone *)zone
  30. {
  31.  return self;
  32. }
  33.  
  34. - (id)retain
  35. {
  36.  return self;
  37. }
  38.  
  39. - (NSUInteger)retainCount
  40. {
  41.  return NSUIntegerMax;
  42. }
  43.  
  44. - (void) release
  45. {
  46. }
  47.  
  48. - (id)autorelease
  49. {
  50.  return self;
  51. }
  52. @end