objective-c - NSMutableArray 作为实例变量

标签 objective-c cocoa nsmutablearray instance-variables nswindowcontroller

我有一个实例变量,它是 NSMutableArray

@interface SummaryWindowController : NSWindowController {

    NSMutableArray *aBuffer;

使用此方法设置 NSMutableArray(从初始化此对象的对象调用):

- (void)setGlobalStatusArray:(NSMutableArray *)myArray
{
    if (!aBuffer) {
        [myArray retain];
        NSLog(@"aBuffer not init , alloc init now");
        aBuffer = [[NSMutableArray alloc] initWithArray:myArray];
    }
    NSLog(@"--> Received buffer: %@",aBuffer);

}

当该方法运行时,NSLog 显示数组的内容:

2011-08-18 16:00:26.052 AppName[74751:1307] --> Recievied buffer: (
        {
        discription = DiskUsage;
        menu = "<NSMenuItem: 0x1005116e0 Hardware Status>";
        status = Warning;
    },

但是在我使用此实例变量的方法中,它似乎不再被初始化

- (IBAction)refreshButtonClicked:(id)sender
{
    NSLog(@"The user has clicked the update button");
    if (!aBuffer) {
        NSLog(@"refresh button not init");
    }
    NSLog(@"Buffer is currently:%@",aBuffer);
}

当它到达这一点时,我看到以下 NSLog:

2011-08-18 16:04:25.301 AppName[74829:1307] The user has clicked the update button
2011-08-18 16:04:25.303 AppName[74829:1307] refresh button not init
2011-08-18 16:04:25.304 AppName[74829:1307] Buffer is currently:(null)

这向我表明 aBuffer 已经(自动?)释放了?

有什么想法为什么要这样做吗?我一开始以为我有两个不同的对象,其中一个是我通过从原始 Controller 初始化 NSWindowController 创建的:

   @interface AppName_AppDelegate : NSObject 
    NSMutableArray *globalStatusArray;

    @implementation AppName_AppDelegate

    if ( summaryWindow ) {
            [summaryWindow release];
        } // end if
        summaryWindow   = [[SummaryWindowController alloc] initWithWindowNibName:@"SummaryWindow" owner:globalStatusController];
    [summaryWindow showWindow:self];
    [summaryWindow setGlobalStatusArray:globalStatusArray];

还有一个是在 Nib 加载时创建的,相同但不同的对象,但是我现在不认为是这种情况,因为我不再看到重复的 NSLogs,所以我假设它只是 NSMutableArray 的一些基本内存问题( s)?

最佳答案

当您应该保留 aBuffer 时,您却保留了 myArray

关于objective-c - NSMutableArray 作为实例变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7115260/

相关文章:

cocoa - 子类化 NSWindow 悬停调整大小

objective-c - 打开文件 - cocoa

objective-c - 向 NSOutlineView 添加标签元素

ios - append 两个数组给出 [__NSCFArray insertObject :atIndex:]: mutating method sent to immutable object'

ios - 创建核心数据托管对象很困难

iphone - 如何以编程方式调用电话?

objective-c - 填充 NSMutableArray 供以后在 obj-c 中使用

ios - 如何从 NSMutableArray 中删除空字符串数据

ios - 使用rangeOfString和textfield shouldChangeCharactersInRange-删除字符?

ios - 在后台下载期间未调用我的 NSURLSessionDelegate 方法