我的程序的基本结构是用户从UITableView中选择一个项目,该项目与存储的文本文件相对应。然后将文件读入数组和字典中,其中数组有键(我知道我只能从字典本身获取键,这不是我的问题)。从应用程序中读取文件时,iPad应用程序崩溃且没有崩溃日志
然后,该视图将更改为UISplitView,其中主视图具有键,并且详细视图中包含词典中归因于该键的项目。在这种情况下,这是用户选择答案的一系列“是/否”问题。
我的问题是这样的:当我点击UITableView(第一个屏幕)中的单元格时,它工作正常,数据完全读取,等等。当我回到UITableView并再次单击相同的单元格时,程序崩溃。这里是读入,从文件的方法:
-(NSArray *)readFromFile:(NSString *)filePath{
// NSLog(@"Path was: %@", filePath);
NSString *file = [[NSString alloc] initWithContentsOfFile:filePath];
// NSLog(@"File was: %@", file);
NSScanner *fileScanner = [[NSScanner alloc] initWithString:file];
NSString *held;
NSString *key;
NSMutableArray *detailStrings;
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *details = [[NSMutableDictionary alloc] init];
/**
This is where the fun stuff happens!
**/
while(![fileScanner isAtEnd]){
//Scan the string into held
[fileScanner scanUpToString:@"\r" intoString:&held];
NSLog(@"Inside the while loop");
// If it is a character, it's one of the Key points, so we do the management necessary
if ([[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:[[held lowercaseString] characterAtIndex: 0]]){
NSArray *checkers = [[NSArray alloc] initWithArray:[held componentsSeparatedByString:@"\t"]];
NSLog(@"Word at index 2: %@", [checkers objectAtIndex:2]);
if(detailStrings != nil){
[details setObject:detailStrings forKey:key];
[detailStrings release];
}
NSLog(@"After if statement");
key = [checkers objectAtIndex:2];
[keys addObject:(NSString *) key];
detailStrings = [[NSMutableArray alloc] init];
}
else if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:[[held lowercaseString] characterAtIndex: 0]]){
NSArray *checkers = [[NSArray alloc] initWithArray:[held componentsSeparatedByString:@"\t"]];
NSLog(@"Word at index 1: %@", [checkers objectAtIndex:1]);
[detailStrings addObject:[checkers objectAtIndex:1]];
}
}
NSLog(@"File has been read in");
[details setObject:detailStrings forKey:key];
NSArray *contents = [[NSArray alloc] initWithObjects:(NSMutableArray *) keys, (NSMutableDictionary *) details, nil];
[detailStrings release];
return contents;
}
我已经确定
if(detailStrings != nil)
语句中的程序崩溃。我认为这是因为我错过了一些我应该做的内存管理,但没有知道错在哪里。关于这个问题的任何想法,或为什么它没有给我一个日志崩溃?
为什么你检查是否'detailStrings'是'nil' ?它显然是:你在if语句之前从来没有给它赋值。或者我错过了什么? –
调用堆栈或者崩溃的原因是什么? –
经过进一步的思考,我想你有一个释放的指针,'detailStrings'。重新定义它并不会使它成为'零'。 –