Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Global Array?

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?

like image 485
Ethan Mick Avatar asked Mar 19 '10 23:03

Ethan Mick


1 Answers

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...

like image 98
Bill K Avatar answered Oct 27 '22 23:10

Bill K