Iphone NSMutableArray在ViewDidLoad之前被释放

Iphone NSMutableArray在ViewDidLoad之前被释放,iphone,ios,objective-c,cocoa-touch,Iphone,Ios,Objective C,Cocoa Touch,在我的头文件中,我有: @property (nonatomic, retain) NSMutableArray * array1; 在我的initWithNibName方法中,我有: - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (s

在我的头文件中,我有:

@property (nonatomic, retain) NSMutableArray * array1;
在我的
initWithNibName
方法中,我有:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization

    self.array1 = [[NSMutableArray alloc] initWithObjects:@"test", nil];


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DoUpdateLabel:) name:@"DoUpdateLabel" object:nil];

}
return self;
}
DoUpdateLable
方法对数组进行一些更新(添加许多项)。一旦完成并执行ViewDidLoad,我的数组就会消失并变为null,这很烦人,因为我想将其加载到表视图中

这里是
DoUpdateLable
方法的精简版本。由于实际方法包含大量JSON解析和URL请求,我不得不对其进行精简:

-(void)DoUpdateLabel:(NSNotification *) notification
{   
int count = 0;
while(count < 59)
{
    NSString * currentpatname = @"test1";
    if(![currentpatname isEqualToString:@""])
    {
        [self.array1 addObject:currentpatname];
        NSLog(@"array: %@", self.array1);
    }
    count++;
}

NSLog(@"final array: %@", self.array1);
}
-(void)doupDataLabel:(NSNotification*)通知
{   
整数计数=0;
而(计数<59)
{
NSString*currentpatname=@“test1”;
如果(![currentpatname IsequalString:@“”)
{
[self.array1 addObject:currentpatname];
NSLog(@“array:%@”,self.array1);
}
计数++;
}
NSLog(@“最终数组:%@”,self.array1);
}
我有点不明白为什么它没有被保留下来。任何帮助都将不胜感激


谢谢

查看更新的问题,您正在以
//自定义初始化
开始的范围内设置该数组。但您没有注意到的是,
viewDidLoad
实际上是在这一行调用的:
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]

这意味着您的数组在viewDidLoad中仍然为零,因此
DoupDataLabel


我建议您改为在
-viewDidLoad
中初始化该数组,这样您将看到您正在查找的结果。

如果您只是在某个地方使用alloc init,则会调用initWithNibName:bundle:。你可能已经这样做了,但你不应该这样做。删除它,并将问题中的代码放入initWithCoder:,然后它应该可以正常工作。

到目前为止,这并不能真正解决问题。请尝试。这对问题没有影响。无论如何,谢谢。@Adam那么还有一个问题你没有告诉我们。但就您最初显示的代码而言,这确实是问题的一部分。请查看更新后的问题。在viewdidload中,如果我这样做:NSLog(@“viewloadarray:%@”,self.array1);它返回null。你在这里说的全错了。使用_array1引用数组对是否保留数组没有任何影响(事实上,在init方法中,最好使用_array1)。此外,在init方法中未调用viewDidLoad。