Static NSArray of strings - how/where to initialize in a View Controller Static NSArray of strings - how/where to initialize in a View Controller ios ios

Static NSArray of strings - how/where to initialize in a View Controller


Write a class method that returns the array.

+ (NSArray *)titles{    static NSArray *_titles;    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        _titles = @[@"Your Move",                    @"Their Move",                    @"Won Games",                    @"Lost Games",                    @"Options"];    });    return _titles;}

Then you can access it wherever needed like so:

NSArray *titles = [[self class] titles];


You can init it in class method +initialize

static NSArray *_titles_1;@implementation MasterViewController+ (void)initialize {    _titles_1 = @[        @"Your Move",        @"Their Move",        @"Won Games",        @"Lost Games",        @"Options"    ];}@end


You should declare your static array above @implementation.

static NSArray *titles_1 = nil;@implementation ...

And define it in init or awakeFromNib or any other method like viewDidLoad, applicationDidFinishLaunching wherever you want.

- (void)initMethod{ //change the method name accordingly    if (titles_1 == nil){        [self setTitles_1:@[ @"Your Move", @"Their Move", @"Won Games", @"Lost Games", @"Options" ]];    }}