技术开发 频道

iphone 单例全局变量写法

  【IT168 技术】Java代码

  interface MySingleton : NSObject

  {

  ⇒① NSString *testGlobal;

  }

  + (MySingleton *)sharedSingleton;

  ⇒②@property (nonxxxx,retain) NSString *testGlobal;

  @end

  @implementation MySingleton

  ⇒③@synthesize testGlobal;

  + (MySingleton *)sharedSingleton

  {

  static MySingleton *sharedSingleton;

  @synchronized(self)

  {

  if (!sharedSingleton)

  sharedSingleton = [[MySingleton alloc] init];

  return sharedSingleton;

  }

  }

  @end

  interface MySingleton : NSObject

  {

  ⇒① NSString *testGlobal;

  }

  + (MySingleton *)sharedSingleton;

  ⇒②@property (nonxxxx,retain) NSString *testGlobal;

  @end

  @implementation MySingleton

  ⇒③@synthesize testGlobal;

  + (MySingleton *)sharedSingleton

  {

  static MySingleton *sharedSingleton;

  @synchronized(self)

  {

  if (!sharedSingleton)

  sharedSingleton = [[MySingleton alloc] init];

  return sharedSingleton;

  }

  }

  @end

  把①、②、③的地方换成你想要的东西,

  使用例:

  Java代码

  [MySingleton sharedSingleton].testGlobal = @"test";

  [MySingleton sharedSingleton].testGlobal = @"test";

0
相关文章