2017-05-19 7 views
0

"Location"エンティティと "Location"エンティティというエンティティのコードデータには、約50の属性があります。iOS:チェック値がコアデータ内のエンティティの属性と一致していますか?

私は与えられた値を確認するには、次の述語を使用するには、「場所」の実体(私は任意の特定の属性が、エンティティからのすべての属性でそれを一致させる必要はありません)

NSString *[email protected]"Pune"; 
NSArray *enumListForDeleteArray= [[CoreDataModel sharedCoreDataModel] arrayOfRecordsForEntity:@"Location" andPredicate:[NSPredicate predicateWithFormat:@"%@",enumKey] andSortDescriptor:nil forContext:nil]; 

問題から任意の属性と一致します。どのような値 "Pune"が "Location"の属性と一致することを保証する私の述語でなければなりませんか?

NSPredicateの値を推奨します。

EIDT

NSString *enumKey = @"abc"; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Location" inManagedObjectContext:[[CoreDataModel sharedCoreDataModel] getNewManagedObjectContext]];// 
NSMutableArray *orPredicateArray=[[NSMutableArray alloc] init]; 
for(NSDictionary *attributeName in entity.attributesByName) { 
    [orPredicateArray addObject:[NSPredicate predicateWithFormat:@"%K == %@",attributeName,enumKey]]; 
} 
NSCompoundPredicate *combinedPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:orPredicateArray]; 
NSArray *enumKeyReferenceArray= [[CoreDataModel sharedCoreDataModel] arrayOfRecordsForEntity:@"Location" andPredicate:combinedPredicate andSortDescriptor:nil forContext:nil]; 

あり、「ABC」の値は、属性のいずれかになりましたが、それでも私は「場所」のためのオブジェクトを取得していますか?

+0

@Larme。回答ありがとうございますが、私はどのエンティティのすべての属性と比較する単一の述語を見つけませんでした。 –

答えて

3

すべてのプロパティに一致する述語を作成することはできません。 「ワイルドカード」マッチングや「すべてのプロパティ」オプションはありません。あなたの述語はattrib1 == "value" OR attrib2 == "value" OR ...のようなものでなければなりません。

これは可能ですが、非常に遅くなります。チェックするすべてのインスタンスに対して、属性ごとに1つの文字列比較を行う必要があります。あなたの場合、あなたが保存するすべてのオブジェクトの50文字列の比較。可能であれば、結果が良好でないため、要件を変更することを検討する必要があります。

次のような方法で述語を動的に構築できます。上記の理由から私はそれをお勧めしませんが、ゆっくりと動作するはずです。

let entity = // The NSEntityDescription you're fetching 
let stringToMatch = // the string you want to look for 
var attributePredicates = [NSPredicate]() 

for (attributeName, attribute) in entity.attributesByName { 
    if attribute.attributeType == .stringAttributeType { 
     let newPredicate = NSPredicate(format: "%K = %@", attributeName, stringToMatch) 
     attributePredicates.append(newPredicate) 
    } 
} 
let combinedPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: attributePredicates) 
+0

正解ありがとうが、属性の値がまだ得られていない場合は、結果が得られます。私の編集コードをチェックしてください。何か案が? –

+0

フェッチを実行するカスタムメソッドを使用しているため、問題が発生する可能性があります。さもなければ、フェッチから得た属性値を見ることなしに言うのは難しいです。 –

関連する問題