[英]How to verify that NSMutableArray of entity contains object with certain attribute
根据此处的数据模型:摄影者<------- >>照片
当用户去添加Photo
,用户还指定Photographer
拍了什么照片。 当用户决定保存Photo
, Photographer
可能还不存在。 在执行NSFetchRequest以获取Photographer
的所有实例的列表之后,如何检查NSMutableArray(photographerArray)是否包含与用户当前添加的对象具有相同fullName
属性的对象?
天真的方法是简单地获取数组的所有fullName
并检查它是否在其中。
BOOL photographerExist = [[listOfPhotographer valueForKey:@"fullName"] containsObject:enteredFullName];
但是,将其直接放入获取请求中会更容易。 只需添加一个谓词即可。
NSPredicate *fullNamePredicate = [NSPredicate predicateWithFormat:@"fullName = %@", enteredFullName];
fetchRequest.predicate = fullNamePredicate;
如果获取的结果为空,那么摄影师还不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.