NSMutableArray 作为实例变量
我有一个实例变量,它是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您应该保留
aBuffer
时,您却保留了myArray
。You are retaining
myArray
when you should be retainingaBuffer
.尝试在您的界面中使用
。
接下来,
在您的 .m 文件中。之后,
当您设置数组时,请像这样执行:
注意“self”,以便您指定属性。
Try using an
in your interface.
Next,
In your .m file. AFter that,
when you set your array, do it like this:
Note the "self" so that you are specifying the property.