0
我用下面的代碼來填充一個UITableViewController使用NSPredicate來過濾數組造成第二EXC_BAD_ACCESS嘗試
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
NSString *selectedWord;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
ResultDetail *detail = [[[GameStore defaultStore] currentResultDetail] objectAtIndex:[indexPath row]];
if([[detail selectedWord] isEqualToString:@" "])
{
selectedWord = @" selected: n/a";
}
else{
selectedWord = [NSString stringWithFormat:@" selected: %@", [[detail selectedWord] substringFromIndex:4]];
}
[[cell textLabel] setText:[NSString stringWithFormat:@" %@ (score: %d/4)",[detail word],[detail score]]];
[[cell detailTextLabel] setText:selectedWord];
detail = nil;
[detail release];
selectedWord = nil;
[selectedWord release];
return cell;
}
上面的代碼運行良好鑑於currentResultDetail的價值還沒有被請求過。
這裏是currentResultDetail
代碼 - (NSArray的*)currentResultDetail {
NSLog(@"predicate: resultid == %d", [[GameStore defaultStore] currentResultId]);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"resultId == %d", [[GameStore defaultStore] currentResultId] ];
NSArray *filtered = [[[GameStore defaultStore] allResults] filteredArrayUsingPredicate:predicate]; // failed here but only if currentResultId has been requested before.
predicate = nil;
[predicate release];
return [[filtered objectAtIndex:0] resultDetails];
}
上面的代碼失敗上下面的行:
NSArray *filtered = [[[GameStore defaultStore] allResults] filteredArrayUsingPredicate:predicate];
所以基本上,如果用戶請求currentResultId爲1,2,3,4,5,6,7的順序currentResultDetail,它工作正常。但是如果他們要求1,2,3,4,5,1就會崩潰。
任何指針爲什麼會發生這種情況?