2016-12-23 1 views
0

Ich habe eine Eins-zu-viele-Datenbank (Fragen --- >> Buttons) Ich bin hier fest, wie ich damit weitermachen, um auf die Schaltflächen von meinem zugreifen Datenbank:Zugriff auf viele CoreData mit Prädikat

Ich habe den folgenden Code, den ich an meine Questions Entity bekommen kann, aber ich weiß nicht, wie Sie von hier fortfahren. Danke

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)") 
     } 



    } 

Antwort

0

Wäre das der richtige Weg? Ich rate nur und möchte deine Eingabe. Es scheint das richtige Ergebnis zu schaffen, aber ich bin nicht sicher, ob dies der richtige Weg ist, es zu tun ...

dank

[...] 

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)") 
       }  

      } 
Verwandte Themen