[英]IOS/Objective-C: NSPredicate and NULL values in core data fetch
[英]objective c - fetch data (core data)
我有两张桌子Photo
和Photographer
。 下面是表格的结构。
我正在尝试获取所有拍了张叫做“熊猫”的照片的Photographers
的名字。
这是我的代码,但由于某种原因,它返回空值。
NSArray *matchingPhotographers;
NSFetchRequest *fetch = [[NSFetchRequest alloc]init];
NSEntityDescription *desc = [NSEntityDescription entityForName:@"Photographer" inManagedObjectContext:context];
[fetch setEntity:desc];
[fetch setPredicate:[NSPredicate predicateWithFormat:@"photo.name like[c] %@",@"Panda"]];
NSError *error;
matchingPhotographers = [context executeFetchRequest:fetch error:&error];
NSLog(@"Photographers: %@", matchingPhotographers);
难道我做错了什么? 该查询肯定会返回至少1位摄影师。
谢谢
照片是集合,因此您的谓词必须为:
[fetch setPredicate:[NSPredicate predicateWithFormat:@"ANY photo.name like[c] %@",@"Panda"]];
该谓词找到“摄影师”,其中有一张名为“熊猫”的照片(在他的照片收藏中)。
photo
是一对多的关系,因此使用子查询谓词对关系中符合您条件的照片进行计数更容易:
@"SUBQUERY(photo, $p, $p.name ==[c] %@).@count > 0", @"Photo"
注意我也没有使用LIKE,所以它不是正则表达式的比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.