我正在尝试做一个单例 NSMutableArray,但计数函数总是显示 0 个元素。我猜它没有很好地接收对象。
这是我创建的代码。
//VariableStore.h
@interface VariableStore : NSObject
{
NSMutableArray *pruebaGlobal;
}
@property (nonatomic, retain) NSMutableArray *pruebaGlobal;
+ (VariableStore *)sharedInstance;
- (NSMutableArray*)pruebaGlobal;
@end
//VariableStore.m
@implementation VariableStore
@synthesize pruebaGlobal;
+ (VariableStore *)sharedInstance
{
// the instance of this class is stored here
static VariableStore *myInstance = nil;
// check to see if an instance already exists
if (nil == myInstance) {
myInstance = [[[self class] alloc] init];
myInstance.pruebaGlobal = [[NSMutableArray alloc] initWithCapacity:100];
}
// return the instance of this class
return myInstance;
}
- (NSMutableArray*)pruebaGlobal{
return pruebaGlobal;
}
@end
//ViewController.m
NSMutableArray *p = [[VariableStore sharedInstance] pruebaGlobal];
p = [NSArray arrayWithObjects:
[NSMutableArray arrayWithObjects:@"Sí", @"No", nil, nil, nil],
[NSMutableArray arrayWithObjects:@"Súbita", @"Fluctuante", @"Progresiva", nil, nil],
nil];
NSLog(@"%d", [[[VariableStore sharedInstance] pruebaGlobal] count]);