1

Is there a way to define a global NSMutableArray? I'd like to be able to declare the array so I can access/modify it across a series of instance methods. Here is the code that I have below and want to make sure this is how I should do it.

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
    NSMutableArray *sizedWordList;
}

In the .m:

sizedWordList = [[NSMutableArray alloc] init];

- (void)dealloc
{
    [sizedWordList release];
}
locoboy
  • 38,002
  • 70
  • 184
  • 260

4 Answers4

1

It seems to me that you do not want a global variable but, instead, an instance variable. In that case, your declaration:

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
    NSMutableArray *sizedWordList;
}

in the header file is correct. However, in the implementation file, you cannot do the following outside of an instance method (or, if it were indeed a global variable, outside of a class method or a function):

sizedWordList = [[NSMutableArray alloc] init];

It is not legal in Objective-C. The correct place to initialise instance variables is the -init method. Since your class is a subclass of UIViewController, you should override its designated initialiser, -initWithNibName:bundle::

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
    self = [super initWithNibName:nibName bundle:nibBundle];
    if (self) {
        sizedWordList = [[NSMutableArray alloc] init];
    }
    return self;
}

Your -dealloc method is almost correct — remember that you should always send [super dealloc] at the end of your -dealloc method:

- (void)dealloc
{
    [sizedWordList release];
    [super dealloc];
}

Having done that, you can use the array in any other instance method. For instance,

- (void)logWordList {
    NSLog(@"%@", sizedWordList);
}
  • thanks. i actually declared the `sizedWordList` inside another instance method and not in the -init override. Is that ok? – locoboy Apr 19 '11 at 19:29
  • @cfarm54 If you _declare_ a variable inside an instance method then it is a local/automatic variable and it only exists inside that method. On the other hand, you can instantiate your array in a method other than `-init…` but you need to bear in mind that that method must be called before the array becomes usable. –  Apr 19 '11 at 21:59
0

You can create a singleton and share the instance. A singleton will allow only one existing instance of a class to exist. So every accessing code uses the same instance.

Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
  • I'd probably use a singleton as well in this situation, or set the NSMutableArray in the appDelegate if it's something that's appropriate in that class. Whenever I use a singleton I make use of the pattern shown here: http://stackoverflow.com/questions/145154/what-does-your-objective-c-singleton-look-like/145232#145232 – Wolfgang Schreurs Apr 19 '11 at 08:59
  • A singleton is a class of which only 1 instance can exist ever. It's impossible to create multiple instances. Some people consider heavy use of singletons harmful, so one should be careful not to overuse it. – Wolfgang Schreurs Apr 19 '11 at 09:01
0

Sure you can create a class and import it where you want to use it.

header:

#import <Foundation/Foundation.h>

extern NSMutableArray * typesArray;

@interface GlobalVariables : NSObject {

}

@end

implementation

#import "GlobalVariables.h"

@implementation GlobalVariables

NSMutableArray * typesArray;

@end

Now you have acces to typesArray anywheare you import the header

voromax
  • 3,369
  • 2
  • 30
  • 53
Radu
  • 3,434
  • 4
  • 27
  • 38
0

Create a singleton instance of the array in your AppDelegate and access it across your application.

**YourAppDelegate *delegate = (YourAppDelegate *) [[UIApplication sharedApplication] delegate];**

'delegate.sizedWordList' is your global array

Try it out. Good luck.

Hiren
  • 12,720
  • 7
  • 52
  • 72