Richard Topchii
Richard Topchii

Reputation: 8165

One NSDictionary visible everywhere in application

Now I am developing an iOS application which works like this: User scans QR code, App searches for a specific key - > value, it gives out a value to the user.

Currently I have two ViewControllers - the main and "value" ViewController, which is inherited from main. The problem is that if I create NSDictionary in main VC it is not visible in "value" VC. Main VC gives only the string (QR code, the key) through the segue. So, the value VC has to search for key and display the value.

What I ask is some kind of global variable or one DataSource visible across the whole app. Of course, I can implement NSDictionary initialisation inside value ViewDidLoad method and it will work, but this is not the point. New modules are to be added there and the variable has to be global. I googled a lot and got the idea that singleton pattern can be helpful here. I tried to implement it, but no idea how to do. Do I need it, or it is too complex for this kind of DataSource?

Thank you!

Upvotes: 0

Views: 736

Answers (2)

Pochi
Pochi

Reputation: 13459

The basic idea is, you will still need to #include the header file of the place where this dictionary will be. The solution that Naveen proposes means that you will be including the header for the app delegate wherever you want to access it. Whether to use the app delegate for this purpose or not is kinda grayish. Some people often do this, some say its a bad use of it.

The singleton approach means that you will create a class, that will always contain the same information since the init method will return object that was previously created.

For the singleton aproach, imagine I have a database manager class. So in the header of this class (the DatabaseManagerSingleton.h) ill have this:

@interface DatabaseManager : NSObject

+ (DatabaseManager*)sharedInstance;

// Your dictionary
@property (nonatomic,strong) NSMutableDictionary* someDictionary;

The implementation will look like this: (check how "sharedInstance" initializes the object)

@implementation DatabaseManager

#pragma mark - Singleton Methods

+ (DatabaseManager*)sharedInstance {

    static DatabaseManager *_sharedInstance;
    if(!_sharedInstance) {
        static dispatch_once_t oncePredicate;
        dispatch_once(&oncePredicate, ^{
            _sharedInstance = [[super allocWithZone:nil] init];
        });
    }

    return _sharedInstance;
}

+ (id)allocWithZone:(NSZone *)zone {

    return [self sharedInstance];
}

- (id)copyWithZone:(NSZone *)zone {
    return self;
}

- (id)init
{
    self = [super init];
    if (self != nil)
    {
        // Custom initialization
        _someDictionary = [[NSMutableDictionary alloc] init];
    }
    return self;
}

Now, a VERY important thing is that, any place you want to use this object should first include the header:

EDIT: To use it in your code:

1) add the header

#import "DatabaseManager.h"

2) initialize the object

DatabaseManager *databaseManager = [DatabaseManager sharedInstance];

3) do whatever you need

// Initialize the dictionary
databaseManager.someDictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"OBJECT",@"someKey", nil];  // In this case the object is just a NSString.

// Access
[databaseManager.someDictionary objectForKey:@"someKey"];

Upvotes: 3

nprd
nprd

Reputation: 1942

Put as a property on Appdelegate

 @property (nonatomic,strong) NSDictionary * sharedData;

Access anywhere like

NSDictionary *sharedData= ((APPDelegate *) [UIApplication sharedApplication].delegate).sharedData;

Upvotes: 2

Related Questions