NSMutableArray 可见性/保留问题
好吧,我对 NSMutableArray 类有点陌生,我想我错过了一些明显的东西。我有一个对象将 NSMutable 数组传递到我的窗口控制器,就像在 my.m 中一样:
summaryWindow = [[SummaryWindowController alloc] init];
[summaryWindow setGlobalStatusArray:globalStatusArray];
我在 summaryWindow 对象中有接收器方法,如下所示:
-(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)中我有以下内容方法:
- (NSMutableArray *)getGlobalStatusArray
{
return globalStatusArray;
}
现在我在 .h 文件中将 globalStatusArray 声明为
NSMutableArray *globalStatusArray;
所以不应该保留它,因为我正在使用: initWithArray?
当我尝试在另一个 IBAction 方法中访问此值时:
- (IBAction)refreshButtonClicked:(id)sender
{
NSLog(@"The user has clicked the update button");
[ aBuffer addObjectsFromArray: globalStatusArray];
NSLog(@"Buffer is currently:%@",aBuffer);
[tableView reloadData];
}
NSMutable 数组为 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:(
)
我尝试使用自己的方法来获取值,即 [ self getGlobalStatusArray] ,但我错过了一些巨大的东西。仅供参考 aBuffer 也在我的 .h 中声明,
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];
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);
I see the NSLog no problem, and in that same object (summaryWindow) I have the following method:
- (NSMutableArray *)getGlobalStatusArray
{
return globalStatusArray;
}
Now I have globalStatusArray declared in my .h file as
NSMutableArray *globalStatusArray;
So shouldn't This be retained because I am using: initWithArray?
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];
}
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:(
)
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 ,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 albertamg 所指出的,这看起来像一个空数组而不是 nil,并且在正常情况下释放的对象不会神奇地变成 nil。
这是两种不同物体的强烈气味。尝试在您的方法中记录
self
并查看一个实例是否正在获取数组,而另一个实例是否正在与 UI 交互。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.
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.此代码没有做任何有用的事情:
如果旧数组的计数为零,则它会泄漏实际的数组对象。如果计数不为零,则说明它已正确释放。只需进行释放即可,无需费心计数。
您确定 myArray 中确实有东西吗?
乔
This code isn't doing anything useful:
If the count of the old array is zero, it's leaking the actual array object. If the count is not zero, then it's releasing it properly. Just do the release and don't bother counting.
Are you sure there's actually something in myArray?
joe