[英]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.