---
title: "iphone 单例全局变量写法"
author: "niCe"
date: "2010-12-30T14:33:18Z"
tags: []
categories: ["移动开发"]
slug: "iphone"
draft: false
lastmod: "2010-12-30T14:33:18Z"
id: 855839
url: "https://nice.iteye.com/blog/855839"
---

```
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";
```