2012-07-09 59 views
0

我堅持使用nspredicate下面的代碼:這個如何使用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。

回答

0

你可以簡單地將一個var初始化爲零..如果對象數大於零u只需更改var值。我可以從你的解釋中預測這一點。讓我知道如果我在某個地方錯了。