我的简化单例看起来像这样:

@interface MyClass : NSObject {
    NSMutableArray * myArray;
}

+ (MyClass*) instance;
@property(nonatomic,retain)NSMutableArray *myArray;


然后在执行中

static MyClass * myinstance;
@synthesize myArray;

+ (MyClass*) instance {
    if(myinstance == nil)
        myinstance = [[MyClass alloc] init];

    return myinstance;
}


- (id) init {
    if(self = [super init]) {
      myArray = [[NSMutableArray alloc] initWithCapacity:2];
      [myArray addObject:@"Trauma"];
     }
     return self;
}


但是,当我尝试访问它进行tableview时,它总是返回0:

[[[MyClass instance] myArray] count];


有点不确定我在做什么

最佳答案

返回单例实例的方法名为+instance,但是尝试访问单例时,您正在使用+myinstance。您的MyClass实例很可能为零。

关于iphone - 单例类中的NSMutableArray始终返回计数0/从不保留其对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8395945/

10-12 12:36
查看更多