本文介绍了NSMutableArray可见性/保留的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我有一个新的NSMutableArray类,我想我错过了一些明显的东西。我有一个对象将一个NSMutable数组传递给我的窗口控制器,如my.m:

Alright so I am a little new to the NSMutableArray class and I think I am missing something obvious. I have an object pass a NSMutable Array to my window controller like so in my.m:

summaryWindow   = [[SummaryWindowController alloc] init];
[summaryWindow setGlobalStatusArray:globalStatusArray];

我在summaryWindow对象中的接收方法如下:

I have the receiver method in the summaryWindow object as so:

-(void)setGlobalStatusArray:(NSMutableArray *)myArray
{

    if ([myArray count] >0) {
        if (globalStatusArray) {
            [globalStatusArray release];
        }
        globalStatusArray = [[NSMutableArray alloc] initWithArray:myArray];

        NSLog(@"Summary Window Init with new array: %@",globalStatusArray);

我看到NSLog没有问题,在同一个对象(summaryWindow)中我有以下方法:

I see the NSLog no problem, and in that same object (summaryWindow) I have the following method:

- (NSMutableArray *)getGlobalStatusArray
{

    return globalStatusArray;
}



现在我有globalStatusArray在我的.h文件中声明为

Now I have globalStatusArray declared in my .h file as

NSMutableArray   *globalStatusArray;

所以不应该保留,因为我使用:initWithArray?

So shouldn't This be retained because I am using: initWithArray?

当我尝试在另一个IBAction方法中访问这个值:

When I try to access this value in an another IBAction method:

- (IBAction)refreshButtonClicked:(id)sender
{
    NSLog(@"The user has clicked the update button");
    [ aBuffer addObjectsFromArray: globalStatusArray];
    NSLog(@"Buffer is currently:%@",aBuffer);
    [tableView reloadData];
}

NSMutable数组为空

The NSMutable array is null

2011-08-18 10:40:35.599 App Name[65677:1307] The user has clicked the update button
2011-08-18 10:40:35.600 App Name[65677:1307] Buffer is currently:(
)

使用我自己的方法来获得值ie [self getGlobalStatusArray],但我失去了一些巨大的东西。 FYI aBuffer也在我的.h中声明,

I have tried using my own method to get the value i.e. [ self getGlobalStatusArray] to but I am missing something huge. FYI aBuffer is also declared in my .h ,

推荐答案

正如albertamg指出的,看起来像一个空数组而不是nil,一个释放的对象在正常情况下不会奇迹般地变为零。

As albertamg noted, that looks like an empty array rather than nil, and a released object doesn't magically become nil under normal circumstances anyway.

这两个不同的对象有强烈的气味。尝试在你的方法中记录 self ,看看一个实例是否正在获取数组,另一个实例正在与UI交互。

This smells strongly of two different objects. Try logging self in your methods and see if one instance is getting the array and another is interacting with the UI.

这篇关于NSMutableArray可见性/保留的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:52