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.
In your AppDelegate.h file -
@property(nonatomic,retain) NSMutableArray *sharedArray;
In AppDelegate.m
@synthesize sharedArray;
In didFinishLaunchingWithOptions -
sharedArray = [[NSMutableArray alloc]init];
Now,
make create shared object of AppDelegate like-
mainDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
and access sharedArray where you want to access using-
mainDelegate.sharedArray
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With