NSFetchRequest返回不需要的对象

时间:2014-01-01 15:38:46

标签: nspredicate nsmanagedobject nsfetchrequest

我有这个获取请求

-(NSArray*)getMeasureDaysForMeasures:(NSArray*)measures startingFromDate:(NSDate*)startingDate{
    if(measures){
        NSFetchRequest* measureRequest=[NSFetchRequest fetchRequestWithEntityName:@"MeasureDay"];
        measureRequest.sortDescriptors=nil;
        NSPredicate* finalPredicate=nil;
        NSArray* orPredicates=[[NSArray alloc]init];
        for(NSString* entity in measures){
            NSString* lowerCaseEntity=[entity lowercaseString];
            NSPredicate* newPredicate=[NSPredicate predicateWithFormat:@"%@ != %@",lowerCaseEntity,nil];
            orPredicates=[orPredicates arrayByAddingObject:newPredicate];
        }
        finalPredicate=[NSCompoundPredicate orPredicateWithSubpredicates:orPredicates];
        NSLog(@"PREDICATE IS : %@",finalPredicate);
        if(startingDate){
            NSPredicate* datePredicate=[NSPredicate predicateWithFormat:@"date >= %@",startingDate];
            finalPredicate=[NSCompoundPredicate andPredicateWithSubpredicates:@[datePredicate,finalPredicate]];
        }
        measureRequest.predicate=finalPredicate;
        AppDelegate* appDelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
        return [appDelegate.documentManagedObjectContext executeFetchRequest:measureRequest error:nil];
    }
    return nil;
}

因此它不应该返回任何WeightD = = null的MeasureDay对象 但是这个NSLog表明它会返回那些对象,并将它们的值视为<故障>。

for(MeasureDay* day in [self getMeasureDaysForMeasures:_arrayOfChosenMeasures startingFromDate:nil]){
    NSLog(@"%@",day.weight);
}

返回

2014-01-01 19:36:57.682  PREDICATE IS : "weight" != nil
2014-01-01 19:36:57.686  <Weight: 0x14e6f6e0> (entity: Weight; id: 0x14eb0320 <x-coredata://09796286-39C5-4F4B-B7E8-81FD47B256D8/Weight/p1> ; data: <fault>)
2014-01-01 19:36:57.688  <Weight: 0x14d69ef0> (entity: Weight; id: 0x14d79c70 <x-coredata://09796286-39C5-4F4B-B7E8-81FD47B256D8/Weight/p5> ; data: <fault>)
2014-01-01 19:36:57.689  (null)
2014-01-01 19:36:57.691  (null)
2014-01-01 19:36:57.692  (null)

我做错了什么?

1 个答案:

答案 0 :(得分:0)

想通了。应该使用这段代码:

 NSPredicate* newPredicate=[NSPredicate predicateWithFormat:@"%K != %@",lowerCaseEntity,nil];