2016-05-05 10 views
1

TL; DR EDIT答え中間テーブルと多くの関係に多くのコアデータ(スウィフト2)

とウェインは完全に答えたように、この私は今、自分の情報を取得する方法である:

let ingredientsToRecipe = recipe.valueForKey("ingredientsToRecipe")! as! NSSet 
for i in ingredientsToRecipe { 
    print(i.valueForKey("amount")!) 
    print(i.valueForKeyPath("ingredient.name")!) 
} 

オリジナル質問

私はCoreDataで中間テーブルの使用方法を理解する大きな問題を抱えています。私は答えをSO検索し、中間テーブルと多対多の関係が、私を助けていないところのいずれかのObjective-Cまたはそれらについてのいくつかのスレッドを発見しました。

私は次のセットアップ(簡体字)があります。今、私は原料の束を使用して新しいレシピを追加したい image

を。 バーガーを考えてみましょう。バーガーは、これは

  • 1 cocumber、
  • 1トマト、
  • 1肉、
  • 2パン

    (おいしい...)で構成されてい私がこれまで試した:

    // Core Data 
    let appDelegate = 
        UIApplication.sharedApplication().delegate as! AppDelegate 
    let managedContext = appDelegate.managedObjectContext 
    let entity = NSEntityDescription.entityForName("Recipe", 
                   inManagedObjectContext:managedContext) 
    // creating a new recipe with name and id 
    let recipe = NSManagedObject(entity: entity!, 
               insertIntoManagedObjectContext: managedContext) 
    recipe.setValue("Burger", forKey: "name") 
    recipe.setValue("B_001", forKey: "id") 
    

    今、私は成分のうちArray:[NSManagedObject](バーガーのように作られた)とDictionaryのamount_IDsを得ました。これは私のレシピと(中間のテーブル上の)成分と結婚しようとしているところです。すべてが何らかの形で上記の作品この

    do { 
        try managedContext.save() 
        print("Saved successfully") 
        self.dismissViewControllerAnimated(true, completion: nil) 
    } catch let error as NSError { 
        print("Could not save \(error), \(error.userInfo)") 
    } 
    

    for i in selectedIngredients { // the ingredient array 
        let ingredientsToRecipe =  NSEntityDescription.insertNewObjectForEntityForName("RecipeIngredient", inManagedObjectContext: managedContext) 
    
        ingredientsToRecipe.setValue(i, forKey: "ingredient") 
        ingredientsToRecipe.setValue(recipe, forKey: "recipe") 
    
        let quantity = Double(quantityDictionary[(i.valueForKey("id") as! String)]!) // the amount-to-ID dictionary 
        ingredientsToRecipe.setValue("\(quantity)", forKey: "quantity") 
    } 
    

    は最後に、私は単純にすべてのものを保存します。しかし、今私は自分のレシピについての情報を取得するのに苦労しています。どのように私は、この特定のバーガーのトマトの量を取得することが出来るのです ?

    のようなものは recipe.valueForKey("RecipeIngredient").valueForKey("amount")のように機能しますが、どの成分がどの成分であるのかわかりません。 何か間違っていますか? どうすればいいですか?

    目標は、食材とレシピを作成し、後でレシピとそれの成分の量(および成分そのもの)に関する情報を表に移入することです。

    ご協力いただきありがとうございます。

答えて

1

中間オブジェクトのパワーは、それはあなたの多対多の関係を取り、複数の1対多の関係にそれを壊すことです。 1対1の関係は容易にナビゲートできます。

だから、あなたからRecipeあなたがRecipeIngredientsの配列を取得することができ、それぞれの1のためにあなたがvalueForKey("amount")valueForKeyPath("ingredient.name")を得ることができます。そして、あなたは、単に返さRecipeIngredientエンティティから股関節量の値を取得

var request = NSFetchRequest(entityName: "RecipeIngredient") 
let predicate = NSPredicate(format: "recipe.name = %@ AND ingredient.name = %@", "burger","tomato") 
request.predicate = predicate 

:あなたはこのような述語を使用してRecipeIngredientでフェッチ要求を作成することができ、特定のレシピのための成分の量を取得するために

+0

Omgありがとうございます。私は私の頭の中にこのようなブロックを持っていた。このように魅力的な作品。二度upvoteできませんが、抱擁を感じてください! – Akaino

1

+0

ありがとうございました! @ワインの応答は私のニーズにも合っています。 – Akaino

関連する問題