When you have an NSArray
object containing NSString
objects in your View Controller, you might want to create a static version of it, especially when the user will only see the same values each time the view appears.
First, add your C array pointers to NSString
constants by following instructions in my earlier post.
In Constants.h
:
extern NSString * const kMenuItems[];
In Constants.m
:
NSString * const kMenuItems[] = { @"ABC", @"123", @"Do Re Me" };
Make sure you import Constants.h
in your view controller class. If you have other constants that span across most of classes, you might want to add Constants.h
into a PCH File (see instructions in my previous post).
Next, add a new method in your view controller class:
+ (NSArray *)topicArray { static NSArray *topics; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ topics = [NSArray arrayWithObjects:kMenuItems count:3]; }); return topics; }
The onceToken
will ensure that your NSArray
object is created once.
Finally, use it in your class’ viewDidLoad
method:
topicArray = [[self class] topicArray];