how and where do I initialize an global NSMutableArray in Xcode 5(如何以及在哪里初始化 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何以及在哪里初始化 Xcode 5 中的全局 NSMutableArray


基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01