如何以及在哪里初始化 Xcode 5 中的全局 NSMutableArray [英] how and where do I initialize an global NSMutableArray in Xcode 5

查看:56
本文介绍了如何以及在哪里初始化 Xcode 5 中的全局 NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试初始化一个全局 NSMutableArray,我可以在以后添加整数.我只需要知道我应该如何以及在哪里初始化我的数组,以便我以后在程序中使用的任何函数都可以访问和更改它.另外我正在使用 Xcode 5,并且知道数组的长度需要为 180.

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 and changed by any function that I use later in my program. Also I am using Xcode 5 and know that the array needs to be 180 in length.

推荐答案

您可以创建一个单例类并在该类上为您的数组定义一个属性.

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

例如:

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

这篇关于如何以及在哪里初始化 Xcode 5 中的全局 NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆