views:

89

answers:

2

Hi guys, How do you access a NSDictionary in different NSViewControllers. The NSDictionary was initialized in my delegate file. Do I have to import my delegate file to each view controller file or do all my views on top of my delegate know of my NSDictionary? Do I possibly need to declare my Dictionary in each file? -Thanks Guy

A: 

The NSDictionary should be exposed as a property in whatever class creates it. You do have to import the header file for every other class that needs to use it (so that they will be able to access the property).

It's generally bad practice to put these sorts of things in the App Delegate (the child classes shouldn't have to rely on the app delegate)--it sounds like it would be better suited for a model class (for more information on the MVC pattern, you can see Apple's documentation).

eman
A: 

If you have your NSDictionary in your application delegate, you can declare it as a property in the delegate. Then, in any file which needs the dictionary object, you can access it as below:

AppDelegate *mydelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

NSDictionary *dict = mydelegate.dictionary;

This is assuming your delegate file name is AppDelegate and your NSDictionary object reference name is dictionary. You would need to import AppDelegate in your files though in order to use the cast.

Hetal Vora