2016-12-23 9 views
0

私は1対多のデー​​タベースを持っています(質問--- >>ボタン) 私はこの方法で私のデータベース:述語で1対多のCoreDataにアクセス

私はQuestionsエンティティにアクセスできますが、ここから進める方法はわかりません。ありがとう

func presentQuestionDetails(questionID :Int) { 

     let coreDataStack = CoreDataStack() 
     managedObjectContext = coreDataStack.persistentContainer.viewContext 

     let fetchRequest: NSFetchRequest<Questions> = Questions.fetchRequest() 

     let myPredicate = NSPredicate(format: "%K == %i", "questionID", questionID) 
     fetchRequest.predicate = myPredicate 

     do { 
      let results = try managedObjectContext!.fetch(fetchRequest) 

      if results.isEmpty { 
       //empty 

       print("empty") 
      } 

      else { 
       let question = results[0] 

       //do something 
       print("got something") 
       questionLabel.text = question.questionTitle 

       for _ in 0..<(question.buttons?.count)! { 


        //I'D LIKE TO LOOP THROUGH MY BUTTONS HERE AND ACCESS MY "buttonTitle" i.e print(buttons.buttonTitle!) 



       } 

      } 


     } catch { 
      // Replace this implementation with code to handle the error appropriately. 
      // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
      let nserror = error as NSError 
      fatalError("Unresolved error \(nserror), \(nserror.userInfo)") 
     } 



    } 

答えて

0

これは正しい方法でしょうか?私はあなたの意見を推測しています。 右の結果を提供しているようだが、私は、これはそれを行うための正しい方法であるかどうかわからないです...

おかげ

[...] 

else { 
       let question = results[0] 

      //do something 
      print("got something") 
      questionLabel.text = question.questionTitle 

       let fetchRequest: NSFetchRequest<Buttons> = Buttons.fetchRequest() 
       let myPredicate = NSPredicate(format: "%K == %i", "questions", questionID) 
       fetchRequest.predicate = myPredicate 

       do { 
        let results = try managedObjectContext!.fetch(fetchRequest) 

        for buttons in results { 
         print(buttons.buttonTitle!) 

        } 
       } 

       catch { 
        let nserror = error as NSError 
        fatalError("Unresolved error \(nserror), \(nserror.userInfo)") 
       }  

      } 
関連する問題