NSMutableArray 作为实例变量

发布于 2024-11-30 05:52:42 字数 2002 浏览 0 评论 0原文

我有一个实例变量,它是 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 已(自动?)释放?

有什么想法为什么要这样做吗?我一开始以为我有两个不同的对象,一个是我通过从原始控制器初始化 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];

另一个是在笔尖加载时创建的,相同但不同的对象,但我现在不认为是这样,因为我不这么认为。不再看到重复的 NSLogs,所以我假设它只是 NSMutableArray 的一些基本内存问题?

I have a instance variable which is a NSMutableArray

@interface SummaryWindowController : NSWindowController {

    NSMutableArray *aBuffer;

The NSMutableArray is set using this method (called from the object that init'ed this object):

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

}

The NSLog shows the contents of the array when that method runs:

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

But in my method that uses this instance variable it no longer seems init'ed

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

As when it gets to this point I see the following 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)

Which would indicate to me that aBuffer has been (auto?)released?

Any ideas why this would be doing this? I thought at first I had two distinct objects, one which I created by initing NSWindowController from the original controller:

   @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];

And one that was created when the nib load, identical but different objects however I now don't think thats the case as I don't see duplicate NSLogs any more, so I assume its just some basic memory issue with NSMutableArray(s)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新雨望断虹 2024-12-07 05:52:42

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

You are retaining myArray when you should be retaining aBuffer.

总以为 2024-12-07 05:52:42

尝试在您的界面中使用

@property (nonatomic, retain) NSMutableArray *aBuffer

接下来,

 @synthesize aBuffer

在您的 .m 文件中。之后,

当您设置数组时,请像这样执行:

    self.aBuffer = [[NSMutableArray alloc] initWithArray:myArray];

注意“self”,以便您指定属性。

Try using an

@property (nonatomic, retain) NSMutableArray *aBuffer

in your interface.

Next,

 @synthesize aBuffer

In your .m file. AFter that,

when you set your array, do it like this:

    self.aBuffer = [[NSMutableArray alloc] initWithArray:myArray];

Note the "self" so that you are specifying the property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文