Kevin Sylvestre
Kevin Sylvestre

Reputation: 38072

Static Variable in Cocoa Class Category

I have a cocoa 'category' for adding inflections (pluralize, singularize, etc.) to NSString. The code requires loading a set of regular expression rules and exceptions from a PLIST into dictionaries and arrays, as well as adding manual exceptions from code. I need a way to persist these data structures (as class members) between multiple calls to the inflection code (all instance methods). I attempted:

+ (NSMutableArray *)uncountables 
{
    static NSMutableArray *uncountables = nil;
    if (uncountables == nil) uncountables = [NSMutableArray array];
    return uncountables;
}

However, it appears to fail occasionally. Does a good way of doing this exist? I don't want to subclass NSString if possible. Thanks.

Upvotes: 2

Views: 561

Answers (3)

kovpas
kovpas

Reputation: 9593

As drawnonward already mentioned, [NSMutableArray array]; returns an autoreleased array. But I don't think, it's a good idea to return non-autoreleased array, because it contradicts with Cocoa memory management conceptions - only alloc, copy and new should be released manually. All other initializations are autoreleased.

So, you should just use

interface:
NSArray *a;

...somewhere in a code...
a = [[NSString uncountables] retain];
...

- (void)dealloc {
    [a release];
}

to get properly retained/released objects.

Upvotes: 0

drawnonward
drawnonward

Reputation: 53689

[NSMutableArray array];

returns an autoreleased array. Use this instead:

[[NSMutableArray alloc] init];

Upvotes: 5

V1ru8
V1ru8

Reputation: 6147

I think this code is OK. I use the same thing a lot for singletons. But be aware that it is not thread safe this way. Maybe you calling it from different threads?

Upvotes: 0

Related Questions