How do I make static initializers in objective-c (if I have the term correct). Basically I want to do something like this:
static NSString* gTexts[] =
{
@"A string.",
@"Another string.",
}
But I want to do this more struct-like, i.e. have not just an NSString for each element in this array, but instead an NSString plus one NSArray that contains a variable number of MyObjectType where MyObjectType would contain an NSString, a couple ints, etc.
Since NSArrays
and MyObjectTypes
are heap-allocated objects, you cannot create them in a static context. You can declare the variables, and then initialize them in a method.
So you cannot do:
static NSArray *myStaticArray = [[NSArray alloc] init....];
Instead, you must do:
static NSArray *myStaticArray = nil;
- (void) someMethod {
if (myStaticArray == nil) {
myStaticArray = [[NSArray alloc] init...];
}
}
This happens to work with constant strings (@"foo"
, etc), because they are not heap-allocated. They are hardcoded into the binary.
It is very important that you make sure that your static initialization is thread-safe (available in iOS 4.1+)!!!
static NSArray *myStaticArray = nil;
- (void) someMethod {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
myStaticArray = [[NSArray alloc] init...]
});
}
here's one way, if you can live with an objc++ translation:
#import <Foundation/Foundation.h>
namespace { // ok, this storage should preferably be in a function/deferred
static struct sa { NSString* const s; NSArray* const a; } r = {
[[NSString alloc] initWithString:@"hello"],
[[NSArray alloc] initWithObjects:@"w", @"o", @"r", @"l", @"d", @"= =", nil]
};
}
int main(int argc, const char* argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSLog(@"\n\n%@...\n\n...\n%@", r.s, r.a);
[pool drain];
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With