Objective-C定义一个全局数组以供多个ViewController使用

我一直在试图从我认为是一个我已经实现的单例类实现全局NSMutableArray。

我可以进入ViewController#2,添加和删除对象到数组中。

但是,当我离开ViewController#2并回来,数据不会持久,我有一个数组0对象。

你觉得我做错了什么?

。H

// GlobalArray.h @interface GlobalArray : NSObject{ NSMutableArray* globalArray; } +(void)initialize; 

.M

 #import "GlobalArray.h" @implementation GlobalArray static GlobalArray* sharedGlobalArray; NSMutableArray* globalArray; +(void)initialize{ static BOOL initalized = NO; if(!initalized){ initalized = YES; sharedGlobalArray = [[GlobalArray alloc] init]; } } - (id)init{ if (self = [super init]) { if (!globalArray) { globalArray = [[NSMutableArray alloc] init]; } } return self; } 

查看控制器#2

 GlobalArray* myGlobalArray; myGlobalArray = [[GlobalArray alloc] init]; //Various add and remove code 

谢谢您的意见。

以下是在应用程序级别共享数据的最佳方法。 单身人士class是一个关键。 Singleton只被初始化一次,剩下的时间共享数据被返回。

 @interface Singleton : NSObject @property (nonatomic, retain) NSMutableArray * globalArray; +(Singleton*)singleton; @end @implementation Singleton @synthesize globalArray; +(Singleton *)singleton { static dispatch_once_t pred; static Singleton *shared = nil; dispatch_once(&pred, ^{ shared = [[Singleton alloc] init]; shared.globalArray = [[NSMutableArray alloc]init]; }); return shared; } @end 

以下是访问/使用共享数据的方式。

 NSMutableArray * sharedData = [Singleton singleton].globalArray; 

你可以在你的ViewController#2中用这个代码创build一个单独的GlobalArray实例:

 GlobalArray* myGlobalArray; myGlobalArray = [[GlobalArray alloc] init]; 

相反,你应该创buildaccessor方法来返回你的共享实例,就像这样:

 // GlobalArray.h @interface GlobalArray : NSObject{ NSMutableArray* globalArray; } +(void)initialize; +(GlobalArray*)sharedInstance; 

与执行:

 // GlobalArray.m // ... your existing code // accessor method +(GlobalArray*)sharedInstance { return sharedGlobalArray; } 

然后从你的ViewController#2调用它:

 GlobalArray* myGlobalArray = [GlobalArray sharedInstance]; 

但是,使用全局variables在视图控制器之间传输数据是不好的做法。 我build议你使用更安全的方法,例如创build一个委托。

要创build一个共享的全局数组,如果这真的是你想要的,只需把它放在头文件中:

 extern NSMutableArray *myGlobalArray; 

这在你的主要源文件中:

 NSMutableArray *myGlobalArray; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { myGlobalArray = [NSMutableArray new]; } 

使用此代码进行设置并获取数组视图,以便添加和删除在控制器本身中单独执行。

 // GlobalArray.h @interface GlobalArray : NSObject @property (nonatomic, strong) NSMutableArray* globalArray; + (id)sharedManager; -(NSMutableArray *) getGlobalArray; -(void) setGlobalArray:(NSMutableArray *)array; #import "GlobalArray.h" @implementation GlobalArray + (id)sharedManager { static GlobalArray *sharedMyManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedMyManager = [[self alloc] init]; }); return sharedMyManager; } - (id)init{ if (self = [super init]) { if (!globalArray) { globalArray = [[NSMutableArray alloc] init]; } } return self; } -(NSMutableArray *) getGlobalArray{ return self.globalArray; } -(void) setGlobalArray:(NSMutableArray *)array{ _globalArray = globalArray; } ------------------------- //get array NSArray * array = [[GlobalArray sharedManager] getGlobalArray]; //set array [[GlobalArray sharedManager] setGlobalArray:array] -------------------------