以下代码尝试在 CSV 文件中搜索由 cell.textlabel.text 给出的字符串
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//create singleton instance
Globals *myGlobals = [Globals sharedGlobals];
//get searchstring form cell
NSString *stringToFind = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
//get Path of csv and write data in string:allLines
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ITILcsv" ofType:@"txt"];
if(filePath){
NSString *wholeCSV = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSArray *allLines = [wholeCSV componentsSeparatedByString:@"\n"];
//declaration
NSArray *currentArray = nil;
NSString *currentSearchString = nil;
//look for searchstring in 4th line of csv, if found write whole line to a singleton-variable
for (int i=0 ; i < [allLines count]; i++){
currentArray = [[allLines objectAtIndex:i] componentsSeparatedByString:@";"];
currentSearchString = [currentArray objectAtIndex:3];
if ([stringToFind isEqualToString:currentSearchString]){
[myGlobals setCurrentLine:currentArray];
}
}
}
在我当前的项目中使用 csv 文件进行了大量工作,我很确定这应该可以工作,但不知何故,当调用函数时,应用程序总是崩溃。
通过一大堆测试,我很确定问题出在以下几行:
currentArray = [[allLines objectAtIndex:i] componentsSeparatedByString:@";"];
currentSearchString = [currentArray objectAtIndex:3];
该程序使用注释掉的这两行,但没有实现所需的功能;)我不知道问题可能是什么?
错误是“main”中的 SIGABRT。
提前谢谢大家。