how and where do I initialize an global NSMutableArray in Xcode 5

后端 未结 4 1919
渐次进展
渐次进展 2020-12-19 10:32

I am trying to initialize a global NSMutableArray that I can add integers to later. I just need to know how and where I should initialize my array so that it can be accessed

4条回答
  •  清歌不尽
    2020-12-19 11:19

    You could create a singleton class and define a property for your array on that class.

    for example:

    // .h file
    @interface SingletonClass : NSObject
    @property (nonatomic,retain) NSMutableArray *yourArray; 
    +(SingletonClass*) sharedInstance;
    @end
    
    // .m file
    
    @implementation SingletonClass
    
    +(SingletonClass*) sharedInstance{
        static SingletonClass* _shared = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _shared = [[self alloc] init];
            _shared.yourArray = [[NSMutableArray alloc] init];
         });
         return _shared;
      }
    
    @end
    

提交回复
热议问题