单例 全局变量

// MyCommon.h: @interface MyCommon class MyCommon : NSObject { int user; }; @property(assign) int user; + (MyCommon *)singleton; @end // MyCommon.m: @implementation MyCommon static MyCommon * MyCommon_Singleton = nil; + (MyCommon *)singleton { if (nil == MyCommon_Singleton) { MyCommon_Singleton = [[MyCommon_Singleton alloc] init]; } return MyCommon_Singleton; } @end The MyCommon singleton is then used anywhere in my application as follows: int user = [MyCommon singleton].user;


posted on 2011-08-25 11:10  风乔  阅读(224)  评论(0编辑  收藏  举报

导航