He revisado (y probado) algunos de los hilos aquí con respecto a Singletons y NSMutableArrays. Soy nuevo en Objective-C así que por favor tengan paciencia conmigo.Forma correcta de crear/usar un Singleton NSMutableArray para Xcode 4
Simplemente quiero crear unas pocas matrices a las que se pueda acceder desde cualquier archivo de vista/.m.
¿Cuál es la mejor (o más concisa) codificación para un Singleton?
A continuación se muestra lo que tengo ahora y me da 1 de advertencia a .m '@implementation' - "Aplicación inadecuada" 1 error en el uso de un archivo de vista .m - "elemento inicializador no es una constante de tiempo de compilación "
Este es el código que tengo ahora - mi archivo GlobalData.h:
#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
mi archivo GlobalData.m:
#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
Y en un archivo .m vista (simplificado):
#import GlobalData.h
GlobalData *globDat=[GlobalData getInstance]; //error occurs here
Puede alguien señalar el problema y si hay una mejor codificación, por favor me ilumine - gracias!
EDITAR
Aquí hay algunos enlaces que he tratado de usar:
Can i have a single NSMutableArray in my multiple views application?
iPhone help with singleton class
Usar Matt Galaghers [singleton skeleton] (http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html). – Till