我坚持在以下代码中使用 nspredicate :
for (int i=0; i<[nodes count]; i++)
{
NSMutableDictionary *dict=[nodes objectAtIndex:i];
NSInteger docid = [[dict objectForKey:@"docid"] integerValue];
NSString *strPred = [NSString stringWithFormat:@"docId == %i",docid];
[request setPredicate:[NSPredicate predicateWithFormat:strPred]];
NSArray* objects=[[context executeFetchRequest:request error:nil] retain];
NSPredicate *objCount = [NSPredicate predicateWithFormat:[objects count]];
if([objects count])//how do i reduce these if else block too using nsprdicate
{
[dict setObject:@"1" forKey:@"isbookthere"];
}
else
{
[dict setObject:@"0" forKey:@"isbookthere"];
}
[objects release];
[nodes replaceObjectAtIndex:i withObject:dict];
}
我也必须删除 ifelse 块,因为如果计数达到 4000,则循环会进行那些导致问题的多次迭代,所以为了降低这种风险,我假设使用 NSPredicate。