我正在使用以下代码填充UITableViewController

- (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,它将崩溃。

关于为什么会发生这种情况的任何指示?

最佳答案

您的谓词将自动释放。不要手动释放它,否则当autoreleasepool试图释放它时会崩溃。

关于iphone - 使用NSPredicate过滤数组会在第二次尝试时导致exc_bad_access,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9388559/

10-10 17:11