我正在尝试制作一个 NSPredicate 来获取特定对象。问题在于 NSPredicate 获取所有对象而不是仅获取特定对象。我做错了什么?
我的代码是这样的:
- (void)fetchDevices {
// Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Songs"];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Songs" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// retrive the objects with a given value for a certain property
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"%d == %d", currentRow, [device valueForKey:@"selectedRow"]];
[fetchRequest setPredicate:predicate];
}
属性和变量:
@property (strong) NSMutableArray *devices;
currentRow = indexPath.row;
[device valueForKey:@"selectedRow"]
[device valueForKey:@"selectedRow"](这是一个 INT,存储在我的核心数据实体中)
最佳答案
很难从您的问题中分辨出来,因为不清楚 Songs
实体上存在哪些属性或您希望如何匹配它们。但是这个谓词的主要问题是:
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"%d == %d", currentRow, [device valueForKey:@"selectedRow"]];
...是它不会尝试匹配 Songs
实体上的任何内容。谓词字符串需要包含您正在获取的实体类型的属性名称之一。或者,如果您在局部变量中有属性名称(也许这就是 [device valueForKey:@"selectedRow"]
是什么?)您需要在谓词。你有一个谓词,其中两边都是整数值,我保证属性 name 不是整数。
同样,如果没有更好地解释您正在尝试做什么,我无法告诉您谓词应该是什么。但是格式字符串的左侧确实需要文字属性名称或匹配属性名称的 %K
才能有效过滤。
关于ios - 谓词获取所有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21121982/