开发者

Exception raised when filtering NSManagedObjects with NSPredicate

In short, I don't think I'm formatting the NSPredicate correctly.

Given an array of NSManagedObjects, I want to construct a new array containing objects where an attribute matches a certain value. In this case, where the attribute status is equal to the value "inactive."

parentObject is the parent entity in my core data object model. An ivar passed in from the previous controller. It's also already been fetched via a开发者_运维技巧 previous controller.

theRelationship is a one-to-many relationship also defined in the model with entity type ChildEntity.

NSSet *theRelatedObjects = [parentObject valueForKey:@"theRelationship"];  
NSArray *unfilteredObjects = [theRelatedObjects allObjects];  
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status == inactive"]; 
NSArray *filteredObjects = [unfilteredObjects filteredArrayUsingPredicate:predicate];   
//at this point NSUnknownKeyException is raised.  
//error message:  the entity ChildEntity is not key value coding-compliant for the key "inactive"  

Thank you for your help!


Do this instead:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status like 'inactive'"];


Is 'status' a string? Try something like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status == %@", @"inactive"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜