如何使用nspredicate?

时间:2012-07-09 10:40:02

标签: iphone

我坚持在以下代码中使用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。

1 个答案:

答案 0 :(得分:0)

你只需要初始化为零的var ..如果对象计数大于零你只需更改var值。我可以从你的解释中预测这一点。如果我错了,请告诉我。