Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to create/use a Singleton NSMutableArray for Xcode 4

I've reviewed (and tried) a bunch of the threads here regarding Singletons and NSMutableArrays. I'm new to Objective-C so please bear with me.

I simply want to create a few arrays that can be accessed from any view/.m file.

What is the best (or most concise) coding for a Singleton?

Below is what I have now and I get 1 warning at .m '@implementation' - "Incomplete implementation" 1 error at usage in a view .m file - "initializer element is not a compile-time constant"

This is the code I have now - my GlobalData.h file:

#import <Foundation/Foundation.h>

@interface GlobalData : NSObject {    
    NSMutableArray *listOfHeadings;
    NSMutableArray *listOfItems1;
    NSMutableArray *listOfItems2;
}    
@property(nonatomic,retain)NSMutableArray *listOfHeadings;
@property(nonatomic,retain)NSMutableArray *listOfItems1; 
@property(nonatomic,retain)NSMutableArray *listOfItems2; 
+(GlobalData*)getInstance;  
@end

My GlobalData.m file:

#import "GlobalData.h"

@implementation GlobalData
@synthesize listOfHeadings;
@synthesize listOfItems1;
@synthesize listOfItems2;
static GlobalData *instance=nil; 

+(GlobalData *)getInstance    
{    
    @synchronized(self)    
    {    
        if(instance==nil)    
        {    
            instance= [GlobalData new];    
        }    
    }    
    return instance;    
}    
@end

And in a view .m file (simplified):

#import GlobalData.h

GlobalData *globDat=[GlobalData getInstance]; //error occurs here

Can someone point out the trouble and if there's better coding, please enlighten me - thanks!

EDIT

Here's a few links I've tried to use:

Can i have a single NSMutableArray in my multiple views application?

iPhone help with singleton class

like image 938
wayneh Avatar asked Jan 19 '12 19:01

wayneh


2 Answers

In this case, you might be doing more than you have to. Granted this certainly isn't always the best solution - but you can put your NSMutableArray as a property in your App Delegate and then easily refer to it from any view. By doing it this way - you aren't locking it in as a 'singleton' but there is a 'singleton instance' of it (this helps a great deal for testability).

I have simplified this process here:

YourAppDelegate.h

@property (nonatomic,retain) NSMutableArray *myArray;

YourAppDelegate.m

@synthesize myArray;

YourViewController.m

YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableArray *myArrayFromAppDelegate = appDelegate.myArray;

From this point - you can do any manipulation on this value.

like image 82
dtuckernet Avatar answered Oct 01 '22 02:10

dtuckernet


Here's the "modern" version of a single method to turn any class into a Singleton (in this case formatted as a code snippet). It works in iOS4.x or higher:

+(<#SingletonClassName#> *) sharedInstance 
{
    static <#SingletonClassName#> *_sharedClient = nil;

    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedClient = [[self alloc] init];
    });

    return _sharedClient;
}

But, do you really need a singleton of a single NSMutableArray? You could use the built-on singleton - your application delegate, which is got to by calling:

MyAppDelegate * appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.myMutableArray addObject:...];
like image 23
Kendall Helmstetter Gelner Avatar answered Oct 01 '22 03:10

Kendall Helmstetter Gelner