简体   繁体   English

如何验证实体的NSMutableArray是否包含具有特定属性的对象

[英]How to verify that NSMutableArray of entity contains object with certain attribute

Based on the data model here: Photographer<------->>Photo 根据此处的数据模型:摄影者<------- >>照片

When the user goes to add a Photo , the user also specifies what Photographer took said picture. 当用户去添加Photo ,用户还指定Photographer拍了什么照片。 When the user decides to save the Photo , there is the possibility that the Photographer doesn't exist yet. 当用户决定保存PhotoPhotographer可能还不存在。 After executing a NSFetchRequest to get a list of all instances of Photographer , how do I check if the NSMutableArray(photographerArray) contains an object that has the same fullName attribute as what the user is currently adding? 在执行NSFetchRequest以获取Photographer的所有实例的列表之后,如何检查NSMutableArray(photographerArray)是否包含与用户当前添加的对象具有相同fullName属性的对象?

The naive way would be to simply get all the fullName of the array and check if it's in it. 天真的方法是简单地获取数组的所有fullName并检查它是否在其中。

BOOL photographerExist = [[listOfPhotographer valueForKey:@"fullName"] containsObject:enteredFullName];

However, it would be way more easier to put that directly in your fetch request. 但是,将其直接放入获取请求中会更容易。 Just add a predicate to it. 只需添加一个谓词即可。

NSPredicate *fullNamePredicate = [NSPredicate predicateWithFormat:@"fullName = %@", enteredFullName];
fetchRequest.predicate = fullNamePredicate;

If the result of the fetch is empty, then the photographer doesn't exist yet. 如果获取的结果为空,那么摄影师还不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM