0

I am trying to implement one static global dictionary that can able to access on all classes. is there any other way other than singleton. i tried a-lot in stack overflow i couldn't get any solution someone help me to sort this problem. This is my code for dictionary.

 NSDictionary *colors = @{@"black": [UIColor blackColor], @"pink": [UIColor purpleColor], @"blue": [UIColor blueColor], @"red": [UIColor redColor], @"yellow": [UIColor yellowColor]};

i am using in my classes with user defualts like this.

 NSString *savedValue = [[NSUserDefaults standardUserDefaults]
                            stringForKey:@"colordatafromthisvc"];
    UIColor *color = colors[savedValue] ?: [UIColor orangeColor];
        _nxtbtnlet.backgroundColor = color;
ios12
  • 3
  • 2

4 Answers4

1

It's always considered best to use a singleton for such purposes.

In case you are pressed to not use singletons, you can use:

Option 1

In the .h file:

extern NSDictionary *GlobalDictionary;

In the .m file:

NSDictionary *GlobalDictionary;

Then, in the initializer function of the corresponding .m file (or any other method that is sure to be invoked after the app has launched), you can have:

GlobalDictionary = @{@"black": [UIColor blackColor], @"pink": [UIColor purpleColor], @"blue": [UIColor blueColor], @"red": [UIColor redColor], @"yellow": [UIColor yellowColor]};

You'll now be able to access GlobalDictionary from any files that includes it's header file, like this:

UIColor *color = [GlobalDictionary objectForKey:@"blue"];

Option 2

Consider saving the UIColor itself to UserDefaults.

Check this out: Saving UIColor to and loading from NSUserDefaults

D V
  • 348
  • 2
  • 10
  • how to access in other viewcontrollers, if i assign it in one vc – ios12 Oct 25 '18 at 08:58
  • The property is to be declared outside any controllers or classes. And the initial value can be assigned to it from any other class also. Then, you would be able to access this without **specifying** any other class. But you would have to obviously import the header to which the **extern** declaration belongs. – D V Oct 25 '18 at 10:03
0

Instead of saving only values you can save the whole dictionary in NSUserDefaults and retrieve it whenever needed.

Use following link for reference.

Faysal Ahmed
  • 149
  • 4
0

You can create a global PCH file, write the macro definition in the PCH file, and then import the global PCH file. These macro definitions can be used in PCH files, #define SavedValue @{@"black": [UIColor blackColor], @"pink": [UIColor purpleColor], @"blue": [UIColor blueColor], @"yellow": [UIColor redColor]}

JackySong
  • 154
  • 1
  • 4
0

Declare you dictionary in app delegate and then you can access in other class.

// AppDelegate.h

//  AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    NSDictionary *colors;

}
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong)  NSDictionary * colors;

and then you appdelegate.m file

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    _colors = @{@"black": [UIColor blackColor], @"pink": [UIColor purpleColor], @"blue": [UIColor blueColor], @"red": [UIColor redColor], @"yellow": [UIColor yellowColor]};

    return YES;
}

Usage:

AppDelegate *appDelgObj = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSDictionary *colorsDict = appDelgObj.colors;
    UIColor *color = colorsDict[_clrStr] ?: [UIColor orangeColor];

try it and let me know if you face any difficulties.

Ref from : https://stackoverflow.com/a/17268510/10556011

Ram
  • 858
  • 1
  • 14
  • 19
  • Thanks @Ram This is what i am expecting. – ios12 Oct 25 '18 at 12:00
  • @ios12 Pretty interesting that you specifically mentioned that you wanted to have something other than the singleton approach, and what you have selected does the same. – D V Oct 25 '18 at 14:30