gpt4 book ai didi

iphone - 单例类中的 NSMutableArray 总是返回计数 0/从不保留其对象

转载 作者:行者123 更新时间:2023-11-28 23:10:34 27 4
gpt4 key购买 nike

我的简化单例是这样的:

@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 实例很可能是 nil。

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com