5

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?

Galen
  • 29,976
  • 9
  • 71
  • 89
buley
  • 28,032
  • 17
  • 85
  • 106
  • 1
    "Instance variable 'blah' accessed in class method." indicates a different problem: it means you have a class method (beginning with "+") that's trying to refer to an instance variable. Since class methods are, by definition, not connected to any particular instance of the class, they don't have access to instance variables. – David Gelhar May 05 '10 at 04:00

4 Answers4

9

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];
}
RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
  • 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? – buley May 05 '10 at 04:00
  • 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. – RedBlueThing May 05 '10 at 04:03
  • 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. – buley May 05 '10 at 04:06
  • No problems. Happy to help :) – RedBlueThing May 05 '10 at 04:07
2

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
  • 27,873
  • 3
  • 67
  • 84
  • What's a singleton class? (Sorry for my ignorance. Again: dirty PHP coder.) – buley May 05 '10 at 04:03
  • 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 May 05 '10 at 04:12
  • Thank you, very clear. Also, thank you for your very helpful comment on my original question above. – buley May 05 '10 at 04:13
  • For more info on the singleton, read this question: http://stackoverflow.com/questions/145154/what-does-your-objective-c-singleton-look-like – John May 05 '10 at 04:23
1

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
  • 6,749
  • 13
  • 51
  • 102
1

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

NSMutableDictionary *gDictionary;

@implementation ...
@end
drawnonward
  • 53,459
  • 16
  • 107
  • 112