2

I want a NSMutableArray global. I have the following setup:

code.h
extern NSMutableArray *global
@interface
@end

code.m
NSMutableArray *global=nil;
@implementation
...
global=variable_mutablearray; //copy variable_mutablearray to global
@end

I'm pretty sure that what I'm doing setting an existing nsmutablearray to the global variable is not correct. What should I be doing?

2
  • 1
    If you are trying to create a truly global object (as opposed to, say, a singleton) in Objective-C you are almost certainly doing something wrong. Can you elaborate on what your goal is? Commented Jul 30, 2012 at 3:00
  • 1
    As above, singleton is almost certainly the way to go. Your copy operation is only a shallow copy as well. You are only copying the memory location as opposed to the contents of the array. Commented Jul 30, 2012 at 3:02

2 Answers 2

3

Globals are not the best thing to use in Objective C: it is much better to use a singleton.

You can do it like this:

Header:

@interface Globals : NSObject
+(NSMutableArray*)array;
@end

Implementation:

@implementation Globals
+(NSMutableArray*)array {
    static NSMutableArray *statArray;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        statArray = [NSMutableArray array];
    });
    return statArray;
}
@end

Usage:

NSMutableArray *ga = [Globals array];
Sign up to request clarification or add additional context in comments.

5 Comments

Note that this implementation is highly thread unsafe. The preferred approach is to use dispatch_once(), which provides for thread-safety.
@ConradShultz You're right, this is the approach that Apple recommends. I changed the implementation, thanks!
Globals should extend : NSObject
@Odelya Thanks, I edited the answer. In the future, when you see glaring mistakes like that, feel free to edit the answer: very often it is much faster than typing up a comment. Thanks again!
@dasblinkenlight you are right - but I feel that I am not an objective-c expert yet, so maybe you would have something in mind which is different than what I think
0

I have to assume the line "global = variable_mutablearray;" is an assignment inside of a method that you call at some point.

This would work but you must keep in mind that the 'ownership' of this object would be questionable at best. Namely every time it is assigned or reassigned the assigner should probably treat the global as if it was an ivar for a class (meaning that you would retain the assignee object, release the global, then assign the assignee to the global). You need to ensure you don't do something like assign with an autoreleased object.

I do agree that a singleton/encapsulating the data some other way would be preferred. But ultimately Objective-C is C and thus inherits the global/shared memory paradigm.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.