I have an app with some views and one dictionary. These views need to make changes in this dict. How can I make it global so that I can access it from any view?
-
Can't you make it a part of a static class ? – mohkhan Jun 24 '13 at 05:18
-
@mohkhan: and what is a static class in Objective-C? – vikingosegundo Jun 24 '13 at 05:19
-
1you can do this stuff using Delegate class. create Dictionary in to Delegate class and use this dictionary using Delegate Object :) – Nitin Gohel Jun 24 '13 at 05:19
-
@vikingosegundo I don't think that's an abuse. I think creating a separate class just for holding one singleton object is way worse. – Jun 24 '13 at 05:24
-
@H2CO3, it might not be as bad as misusing NSUserDefaults, but I'd prefer a sharedInstance of something that also is called to give a clue, what it is supposed to do, like register… – vikingosegundo Jun 24 '13 at 05:53
-
Just wondering, is this really the way you do this in Objective C? Use global data or anti-patterns like singletons? No use of inversion of control, dependency injection, for example? Looks a little odd to me... – stef77 Jun 24 '13 at 05:58
-
If the dictionary deserves to be a separate object, e.g. looking up, deleting, complex loading, etc, make it a singleton. – Khanh Nguyen Jun 24 '13 at 05:59
5 Answers
Make use of Singleton Class
Singleton Class: it's an extremely powerful way to share data between different parts of code without having to pass the data around manually.
This link will help you know more about it!

- 2,919
- 21
- 35
Create it as an instance variable of a globally visible and persistent object, such as the application delegate. For example:
@interface AppDelegate: NSObject <UIApplicationDelegate> {
NSMutableDictionary *dict;
}
@property (nonatomic, strong) NSMutableDictionary *dict;
// implementation:
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(id)opts
{
// ...
dict = [[NSMutableDictionary alloc] init];
// ...
return YES;
}
Then use it like this:
AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableDictionary *global = appDel.dict;
// use `global' here
You can either use singleton object for that or place your dictionary in AppDelegate and use it from there importing AppDelegate.h in other classes.

- 2,054
- 3
- 17
- 33
Refer this question
Create NSMutableDictionary that will be available from everywhere in the app
The answer for the given question using Singleton pattern

- 1
- 1

- 42,757
- 9
- 93
- 110
You can use NSUserDefault
.
Make any dictionary and set it in UserDefault.
Change the value of dictionary anywhere you want and set that dictionary to userdefault.
Then You can access the value of userdefault which contains your dictionary anywhere you want.
Try like this ;
[[NSUserDefault standarduserdefault]setObject:"your dictonary" forKey:@"KEY_NAME"];
and then access this dictionary as ;
[[NSUserDefault standarduserdefault]dictionaryForKey:@"KEY_NAME"];
Hope so this will help you!!

- 2,939
- 5
- 36
- 65
-
3`NSUserDefault` is supposed to be used with small user data. Putting the whole dictionary into it is not a good idea. Actually it depends on what OP wants with his "dictionary". – Khanh Nguyen Jun 24 '13 at 05:57
-
1And what if objects in the dictionary are not serializable? Then you can't put the into `NSUserDefaults`. And also, why even bother using the filesystem (`NSUserDefaults` persists to a property list) if all we need is an in-memory object? This is a very bad piece of advice. – Jun 24 '13 at 06:00
-
@KhanhNguyen - you are right. It depends on what OP wants with his "dictionary". But it is the good and right idea to use NSUserDefault if his dictionary is of small contents. – Rohan Jun 24 '13 at 06:00
-
@Rohan I thought he meant a real-life dictionary. Now I realized he could mean a `NSDictionary`. – Khanh Nguyen Jun 24 '13 at 06:03
-
-
Still, it is a bad idea to **save** data that doesn't needs saving just so that you can access it anywhere. Even though singletons often lead to bad design that is the way to go! – Groot Jun 24 '13 at 06:45
-
Very bad advice — especially if it needs to hold instances of custom classes. – vikingosegundo Jun 24 '13 at 08:40