views:

495

answers:

6

So, I read this post, and it's pretty much exactly what I was looking for. However... it doesn't work. I guess I'm not going to go with the singleton object, but rather making the array in either a Global.h file, or insert it into the _Prefix file.

Both times I do that though, I get the error:

Expected specifier-qualifier-list before 'static'

and it doesn't work. So... I'm not sure how to get it to work, I can remove extern and it works, but I feel like I need that to make it a constant.

The end goal is to have this Mutable Array be accessible from any object or any file in my project. Help would be appreciated!

This is the code for my Globals.h file:

#import <Foundation/Foundation.h>

static extern NSMutableArray * myGlobalArray;

I don't think I need anything in the implementation file. If I were to put that in the prefix file, the error was the same.

EDIT

So, I removed the .m file from Globals, and I just have the code about in Globals.h. Assuming I am going to continue with this terrible practice of having global variables (I know it's bad, I just want to test this out), I now have a new error. It says:

"Multiple storage classes in declaration specifiers"

If I remove "extern" it works and if I remove "static" it works, but having both doesn't... what now?

*Double Edit*

Aright, so I've tried adding the array to my UIApplication Delegate, but I'm doing it wrong because it isn't working. Could someone give me some example code as to where to place it an access it? I don't know if it should go in the implementation, or somewhere else, and once the array is initialized how to access it from the other files... Do I set a new variable to the array, or something?

+1  A: 

Just a general programming suggestion--don't share an array. You have no control over it and it will be virtually impossible to trace if something changes it at a time and in a way you aren't expecting.

Instead, create an object with the array inside it and make that object a singleton (or better yet, make a factory for it).

Whenever you want to modify your array, call methods on the object to do so. If you do this, I bet you will find a lot of redundant code you can factor into this object (for instance, searching the array for a value--make a "search" method in the object instead and pass in a value).

It may seem like a lot of work you shouldn't have to do, but you'll find it's fairly fun work, and you should find that you DO have to do it once you see how much code belongs in this object...

Bill K
+1  A: 

The two (main) ways of making an array global are separate -- either you have a class with a method

static NSMutableArray *foo;
+(NSMutableArray *)foo {
    return foo;
}

(in the .m file) with the static piece NOT in the header file, or just

static extern NSMutableArray * myGlobalArray;

with out the singleton wrapper (which I think is better as it saves you from having an extra bit of unnecessary code)

Either way, it is still a bad practice that I would try to avoid.

Jared P
A: 
#import <Foundation/Foundation.h>

static extern NSMutableArray * myGlobalArray;

@interface Globals : NSObject {
}


@end
Tuomas Pelkonen
+1  A: 

In general, the presence of a "Globals.h" file is a bad smell that there's an antipattern at work.

I would even advise against Bill K's advice and not use a Singleton pattern at all.

Instead, create the array in your app delegate, and pass it to your root view controller(s), and along the hierarchy to the components that need access to it.

Shaggy Frog
+1  A: 

Just add the array as a property of the application delegate, and access it like:

[[UIApplication sharedApplication] myArray];
chpwn
A: 

This is what I was looking for:

http://derekneely.com/tag/app-delegate/

Thank you for pointing me in the right direction!

Wayfarer