ITeye博客

iphone 单例全局变量写法

niCe · 2010-12-30 14:33 · 代码 · 移动开发 · Markdown

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

 

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

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。