单例 全局变量

// 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;


原文地址:https://www.cnblogs.com/zzxap/p/2175611.html