In a Master-Detail app I\'d like to display a TableView with 5 sections titled:
You can't instantiate objects in this way, you can only declare them in interfaces. Do the following:
static NSArray *_titles_2;
@interface MasterViewController () {
NSMutableArray *_games;
}
@end
@implementation MasterViewController
-(void)viewDidLoad()
{
_titles_2 = @[
@"Your Move",
@"Their Move",
@"Won Games",
@"Lost Games",
@"Options"
];
}
@end
I prefer to use Objective-C++,change filename from xxx.m
to xxx.mm
,and the initialize of NSArray will happen at runtime
no dispatch_once
,no class method,just write it in one line:
static NSArray *const a = @[@"a",@"b",@"c"];
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];