Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 调用NSMutableView中显示时丢失的NSMutableArray元素_Macos_Cocoa_Osx Snow Leopard_Nstableview_Xcode3.2 - Fatal编程技术网

Macos 调用NSMutableView中显示时丢失的NSMutableArray元素

Macos 调用NSMutableView中显示时丢失的NSMutableArray元素,macos,cocoa,osx-snow-leopard,nstableview,xcode3.2,Macos,Cocoa,Osx Snow Leopard,Nstableview,Xcode3.2,我试图将数组的内容显示到表视图中。表视图只有一列。我的类是RKCandidate,它是NSWindowController的子类。RKCandidate也是IB中“nib”文件的所有者。我已选择该文件的所有者作为tableview的数据源 我正在使用这些方法来更新tableview - (id) initWithClient:(TextInputController *) client { self = [super initWithWindowNibName:@"RKCandidate"];

我试图将数组的内容显示到表视图中。表视图只有一列。我的类是RKCandidate,它是NSWindowController的子类。RKCandidate也是IB中“nib”文件的所有者。我已选择该文件的所有者作为tableview的数据源

我正在使用这些方法来更新tableview

- (id) initWithClient:(TextInputController *) client
{
 self = [super initWithWindowNibName:@"RKCandidate"];
 if (self != nil) {
  _client = client;

 }
 return self;
}

- (void) dealloc
{
 [optionsArray release];
 [super dealloc];
}
- (void) updateCandidate {

 NSLog(@"updateCandidate Called.\n");

 optionsArray = [_client composedStringArray:self];
 [candidateOptions reloadData];

 NSLog(@"Retain Count of OptionsArray: %i\n",[optionsArray retainCount]);
 NSLog(@"Object Count of OptionsArray: %i\n",[optionsArray count]);
 NSLog(@"Address of Object: %i\n",[optionsArray objectAtIndex:0]);
 NSLog(@"Object: %@\n",[optionsArray objectAtIndex:0]);
 //NSString *benChar = [optionsArray objectAtIndex:0];
 //NSLog(@"Object Retain Count: %@i\n",[benChar retainCount]);
  }
#pragma mark TableView Data Source Methods
#pragma mark -

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView {

 NSLog(@"Retain Count of OptionsArray: %i\n",[optionsArray retainCount]);
 NSLog(@"Object Count of OptionsArray: %i\n",[optionsArray count]);
 NSLog(@"Address of Object: %i\n",[optionsArray objectAtIndex:0]);
 NSLog(@"Object: %@\n",[optionsArray objectAtIndex:0]);
 //NSLog(@"Object Retain Count: %@i\n",[[optionsArray objectAtIndex:0] retainCount]);

 return [optionsArray count];
}

- (id)tableView:(NSTableView *)aTableView 
     objectValueForTableColumn:(NSTableColumn *)aTableColumn 
    row:(NSInteger)rowIndex {

 NSLog(@"Retain Count of OptionsArray: %i\n",[optionsArray retainCount]);
 NSLog(@"Object Count of OptionsArray: %i\n",[optionsArray count]);
 NSLog(@"Address of Object: %i\n",[optionsArray objectAtIndex:0]);
 NSLog(@"Object: %@\n",[optionsArray objectAtIndex:0]);
 //NSLog(@"Object Retain Count: %@i\n",[[optionsArray objectAtIndex:0] retainCount]);

 return [optionsArray objectAtIndex:rowIndex];
}
选项数组通过客户端的简单方法更新。方法如下:

-(NSMutableArray *) composedStringArray:(id)sender {

 NSLog(@"returning composedStringArray.\n");

 return convertBufferArray;
}
convertBufferArray是一个可变数组

当第一次调用updateCandidate方法时。它工作得很好。但当我在更新客户机内部的convertBufferArray数组后第二次调用它时,问题就开始了

当从第一次调用updateCandidate方法时,控制台日志如下所示:

2010-11-20 09:03:35.385 Input Method Tester[7225:a0f] Retain Count of OptionsArray: 1
2010-11-20 09:03:35.385 Input Method Tester[7225:a0f] Object Count of OptionsArray: 1
2010-11-20 09:03:35.386 Input Method Tester[7225:a0f] Address of Object: 1372384
2010-11-20 09:03:35.386 Input Method Tester[7225:a0f] Object: ∆
2010-11-20 09:03:35.392 Input Method Tester[7225:a0f] Retain Count of OptionsArray: 1
2010-11-20 09:03:35.392 Input Method Tester[7225:a0f] Object Count of OptionsArray: 1
2010-11-20 09:03:35.393 Input Method Tester[7225:a0f] Address of Object: 1372384
2010-11-20 09:03:35.393 Input Method Tester[7225:a0f] Object: ∆
2010-11-20 09:03:35.397 Input Method Tester[7225:a0f] Retain Count of OptionsArray: 1
2010-11-20 09:03:35.397 Input Method Tester[7225:a0f] Object Count of OptionsArray: 1
2010-11-20 09:03:35.398 Input Method Tester[7225:a0f] Address of Object: 1372384
2010-11-20 09:03:35.399 Input Method Tester[7225:a0f] Object: ∆
表视图显示数组的内容

但是,当我向对象添加另一个字母时,当调用-(id)tableView:(NSTableView*)aTableView objectValueForTableColumn:(NSTableColumn*)aTableColumn row:(NSInteger)rowIndex时,字符串将被替换为空字符串。日志如下

2010-11-20 09:03:38.349 Input Method Tester[7225:a0f] returning composedStringArray.
2010-11-20 09:03:38.350 Input Method Tester[7225:a0f] Retain Count of OptionsArray: 1
2010-11-20 09:03:38.351 Input Method Tester[7225:a0f] Object Count of OptionsArray: 1
2010-11-20 09:03:38.352 Input Method Tester[7225:a0f] Address of Object: 1372384
2010-11-20 09:03:38.353 Input Method Tester[7225:a0f] Object: ƍ
2010-11-20 09:03:38.354 Input Method Tester[7225:a0f] Retain Count of OptionsArray: 1
2010-11-20 09:03:38.354 Input Method Tester[7225:a0f] Object Count of OptionsArray: 1
2010-11-20 09:03:38.354 Input Method Tester[7225:a0f] Address of Object: 1372384
2010-11-20 09:03:38.355 Input Method Tester[7225:a0f] Object: ƍ
2010-11-20 09:03:38.358 Input Method Tester[7225:a0f] Retain Count of OptionsArray: 1
2010-11-20 09:03:38.358 Input Method Tester[7225:a0f] Object Count of OptionsArray: 1
2010-11-20 09:03:38.359 Input Method Tester[7225:a0f] Address of Object: 1372384
2010-11-20 09:03:38.359 Input Method Tester[7225:a0f] Object: 
正如您在上面日志的列表行中看到的,字符串为空。tableView将变为空

我做错什么了吗?我已经尝试过释放并保留选项array,但结果是一样的。我还尝试将composedBufferArray的内容复制到选项数组中。我试着把

optionsArray = [_client composedStringArray:self];
进入init方法。所有情况下的结果都是相同的

请帮忙。过去两天我都没能解决这个问题。请帮忙

很抱歉,我无法发布图像或链接,因为我是全新的堆栈溢出

问候,

莱扬首先:

不要调用重新计数 它对于调试是无用的,并且返回的数字通常是完全误导的。对于初学者来说,对象永远不能为保留计数返回0,因此,尝试使用
retainCount
查找过度释放的对象是完全徒劳的

您的bug看起来像是内存管理问题。它没有崩溃只是运气

您应该阅读几次文档(直到它被理解——当我开始w/Objective-C时,我发现阅读几次语言文档是非常有价值的)

您的
-init
方法看起来可疑<代码>\u client=client将不会保留
\u客户端

您还应该在代码中使用“构建和分析”。llvm静态分析器将识别代码中的大多数内存管理问题


最后,学习使用僵尸检测仪。有关更多信息,请参阅部分答案。

非常感谢bbum的答案。后来我发现,在代码的另一部分中,我正在重置字符串。这两个代码并行运行。结果,花了一些时间才发现错误。