5

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.

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
user2975241
  • 73
  • 2
  • 7

4 Answers4

4

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
iSmita
  • 1,292
  • 1
  • 9
  • 24
  • Where do I put the mainDelegate = . . .? Does it also go in the didFinishLaunchingWithOptions? – user2975241 Nov 21 '13 at 01:07
  • mainDelegate is an object of your AppDelegate. In yourViewController.h file initialise AppDelegate *mainDelegate; and in yourViewController.m file's viewDidLoad - mainDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; In this way you can get copy of AppDelegate with sharedArray.Try this. – iSmita Nov 21 '13 at 04:45
  • I get the error "No visible @interface for 'AppDelegate' declares the selector 'delegate' – Marko Jul 13 '14 at 18:48
4

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
Marco
  • 1,276
  • 1
  • 10
  • 19
  • That's the way to do it. It is quite a bad practice to add all kinds of stuff to the app delegate as was suggested. I think you may have to assign "_shared.yourArray" instead. – gnasher729 Feb 17 '14 at 14:12
  • can I use this in a non ARC? – Jongers Jun 01 '16 at 09:35
3

Creating a Singleton class is the better option for you. In this singleton class, you can initialize the array. Later, you can access this array from any class by using this singleton class. A great benefit is you dont need to initialize the class object everytime. You can access the array using a sharedObject.

Below is a tutorial for Singletons in objective C

http://www.galloway.me.uk/tutorials/singleton-classes/

manujmv
  • 6,450
  • 1
  • 22
  • 35
  • I followed the tutorial and initialized my array. I then tried to replace an object in the array using: `[idArray replaceObjectAtIndex:nextIndex withObject:[NSString stringWithFormat:@"red"]];` Next I tried to print my array in the console using nslog but it returns null; so I am still initializing the array wrong or am I using the replaceObjectAtIndex wrong? – user2975241 Nov 21 '13 at 00:52
  • if your array returns null, then you may not be initialized the array – manujmv Nov 21 '13 at 03:28
0

You can initialise your array in app delegate's application:didFinishLaunchingWithOptions: method, as this is called pretty much immediately after your app is launched:

// In a global header somewhere
static NSMutableArray *GlobalArray = nil;

// In MyAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    GlobalArray = [NSMutableArray arrayWithCapacity:180];
    ...
}

Alternatively, you could use lazy instantiation:

// In a global header somewhere
NSMutableArray * MyGlobalArray (void);

// In an implementation file somewhere
NSMutableArray * MyGlobalArray (void)
{
    static NSMutableArray *array = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        array = [NSMutableArray arrayWithCapacity:180];
    });
    return array;
 }

You can then access the global instance of the array using MyGlobalArray().

However, this is not considered good design practice in object-oriented programming. Think about what your array is for, and possibly store it in a singleton object that manages related functionality, rather than storing it globally.

Greg
  • 9,068
  • 6
  • 49
  • 91