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];
}

4 Answers 4

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks. i actually declared the sizedWordList inside another instance method and not in the -init override. Is that ok?
@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.
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.

2 Comments

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: stackoverflow.com/questions/145154/…
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.
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.

Comments

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

1 Comment

super bro...u r a genius

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.