The basic structure of my program has the user select an item from a UITableView, which corresponds to a stored text file. The file is then read into an array and a dictionary, where the array has the keys (I know I can just get the keys from the dictionary itself, this isn’t my question).
The view is then changed to a UISplitView where the master view has the keys, and the detail view has the items in the dictionary attributed to that key. In this case, it’s a series of “Yes/No” questions that the user selects the answer to.
My problem is this: When I click on a cell in the UITableView (first screen), it works fine, the data is read in perfectly, and so on. When I go back to the UITableView and click on the same cell again, the program crashes. Here is the read-in-from-file method:
-(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;
}
I’ve determined that the program crashes inside the
if(detailStrings != nil)
statement. I figure this is because I’m missing some memory management that I am supposed to be doing, but don’t have the knowledge of where it’s going wrong. Any ideas as to the problem, or why it is crashing without giving me a log?
detailStringsis not initialized when you enter thewhileloop. When you declareNSMutableArray *detailStrings;inside a method,detailStringsis not automatically set tonil. So when you doit enters the if statement and since it is not initialized, it will crash when you access
detailStrings.Another thing is that
detailStringswon’t be initialized if it enters theelsepart of the loop first. That will cause a crash too. So based on your requirement, either door initialize it before you enter the
whileloop.