views:

750

answers:

4

I've been trudging through some code for two days trying to figure out why I couldn't fetch a global NSMutableArray variable I declared in the .h and implemented in .m and set in a the viewDidLoad function.

It finally dawned on me: there's no such thing as a global variable in Objective-C, at least not in the PHP sense I've come to know. I didn't ever really read the XCode error warnings, but there it was, even if not quite plain English: "Instance variable 'blah' accessed in class method."

My question: What am I supposed to do now? I've got two View Controllers that need to access a central NSMutableDictionary I generate from a JSON file via URL. It's basically an extended menu for all my Table View drill downs, and I'd like to have couple other "global" (non-static) variables.

Do I have to grab the JSON each time I want to generate this NSMutableDictionary or is there some way to set it once and access it from various classes via #import? Do I have to write data to a file, or is there another way people usually do this?

A: 

There are global variables in Obj-C, you instantiate them in the App Delegate.

But onto your problem you probably want to pass the NSMutableDictonary when you instantiate the new view controller like [UIView alloc] initWithDictionary:(NSMutableDictionary *)dict; if you know what I mean.

In your example are you having a single class calling another class? I would think there is some kind of controller that determines which view controller to display and that would be the place to access and pass the dictionary

Rudiger
+3  A: 

If you have two view controllers that access the shared NSMutableDictionary, can you pass a pointer to the common dictionary into their respective init messages?

So in your AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
  // the app delegate doesn't keep a reference this, it just passes it to the 
  // view controllers who retain the data (and it goes away when both have been released)
  NSMutableDictionary * commonData = [[NSMutableDictionary new] autorelease];

  // some method to parse the JSON and build the dictionary
  [self populateDataFromJSON:commonData];

   // each view controller retains the pointer to NSMutableDictionary (and releases it on dealloc)
   self.m_viewControllerOne = [[[UIViewControllerOne alloc] initWithData:commonData] autorelease];
   self.m_viewControllerTwo = [[[UIViewControllerTwo alloc] initWithData:commonData] autorelease];
}

And in your respective UIViewControllerOne and UIViewControllerTwo implementations

- (id)initWithData:(NSMutableDictionary*)data
{
    // call the base class ini
    if (!(self=[super init]))
        return nil;

    // set your retained property
    self.sharedData = data;
}

// don't forget to release the property
- (void)dealloc {
    [sharedData release];
    [super dealloc];
}
Cannonade
Interesting! I didn't know you could alloc a UIViewController class with initWithData. I've read in a couple places that doing this in AppDelegate is kind of hackish. Is that malarky?
editor
the initWithData message is not a standard message in UIViewController. It is something you will need to add to your UIViewControllers to handle getting data passed in (so you could add whatever params you like to this messages). I'll amend my answer to clarify.
Cannonade
Thanks. Pretty sure this is the best answer I'll get. Still trying to wrap my head around it. Hopefully you won't mind if I ask some follow up questions if they're relevant.
editor
No problems. Happy to help :)
Cannonade
A: 

Just make a global variable. Global means outside any scope.

NSMutableDictionary *gDictionary;

@implementation ...
@end
drawnonward
That would be awesome but it doesn't seem to be working for me. I'll keep trying.
editor
A: 

There are, in fact, a bunch of ways you can do this (without resorting to something extreme like writing to a file). You can create a "global" by using:

  1. Old-fashioned C global variables (externs)
  2. A Singleton class
  3. instance variables on the Application delegate

But all of these approaches make your view controller less modular (because they depend on reaching "outside" to find the global data), so the best approach is probably to make the dictionary a property of the view controller class that must be explicitly set by the caller (either within an initWithDictionary: method, or with a separate setter).

David Gelhar
What's a singleton class? (Sorry for my ignorance. Again: dirty PHP coder.)
editor
A "Singleton" is just a fancy name for a class that instantiates only once instance (I'll add a link to my answer). For example you might have a `MySingleton` class with a `sharedInstance` class method that returns that (single) instance. You could then call methods on the shared instance like: `[[MySingleton sharedInstance] setFoo:@"foo"]`
David Gelhar
Thank you, very clear. Also, thank you for your very helpful comment on my original question above.
editor
For more info on the singleton, read this question: http://stackoverflow.com/questions/145154/what-does-your-objective-c-singleton-look-like
John