客观c - 取数据(核心数据)
问题描述:
我有两个表Photo
和Photographer
。 下面是表格的结构。客观c - 取数据(核心数据)
我想取谁采取了一个名为“熊猫”的照片全部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名摄影师。
感谢
答
photo
是一个一对多的关系,所以它更容易使用子查询谓词计算的关系相匹配,你的标准照片:
@"SUBQUERY(photo, $p, $p.name ==[c] %@)[email protected] > 0", @"Photo"
注意,我还没有使用所以它不是一个正则表达式比较。
+0
另外,'photo'会更好地命名为'photos',所以你记得它是一对多 – Wain 2015-03-13 07:53:06
答
照片是一个集合,所以你的前提必须是:
[fetch setPredicate:[NSPredicate predicateWithFormat:@"ANY photo.name like[c] %@",@"Panda"]];
该断言认定“摄影师”,它必须在列表中的一个照片(他的照片的集合中)与像“熊猫”这个名字。
第一眼看不出有什么问题。你确定它是熊猫/熊猫? – 2015-03-13 07:51:20