Modern way to register NSUserDefaults

Published by malhal on

Here is a modern way to register user defaults. It uses dispatch_once to ensure the registration only happens once and performs it lazily on the first access. Keeps your defaults all on the one place. Also it is implemented as a global block in the class.

NSInteger (^countBeforeAlerting)(void) = ^NSInteger(void) {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"CountBeforeAlerting" : @10}];
    });
    return [[NSUserDefaults standardUserDefaults] integerForKey:@"CountBeforeAlerting"];
};
Categories: Objective-C

0 Comments

Leave a Reply

Avatar placeholder