Home > Mobile >  Swift I can't move data with delegate
Swift I can't move data with delegate

Time:09-27

I'm trying to make a quiz application using the API, I can import the data into the quizManager file, but I can't transfer the data to the viewController, When I try to print in quizManger, I can print, but in viewController does not. how do I move data to viewController?

QuizManager

import Foundation

protocol quizManagerDelegate {
    func didUpdateQuiz(_ Quizmanager: QuizManager ,quiz: QuizModel)
}

struct QuizManager {
    
    var delegate: quizManagerDelegate?
    
    func performRequest(){
        
         let urlString = "https://opentdb.com/api.php?amount=1&type=multiple"        
        if let url = URL(string: urlString){
            
            let session = URLSession(configuration: .default)
            
            let task = session.dataTask(with: url) { data, response, error in

                if error != nil {
                    print(error!)
                    return
                }
                if let safeData = data{
                                        
                    if let quiz = self.parseJSON(quizdata: safeData){
                        delegate?.didUpdateQuiz(self, quiz: quiz)
                    }
                    
                }
            }
            task.resume()
        }
    }
    
    func handle(data: Data?, response: URLResponse?, error: Error?) -> Void {
        
        
    }
    
    func parseJSON(quizdata: Data) -> QuizModel? {
        
        let decoder = JSONDecoder()
        
        do{
            let decodedData = try decoder.decode(Welcome.self, from: quizdata)
            
            let correct = decodedData.results?[0].correct_answer ?? "error"
            let quest = decodedData.results?[0].question ?? "error"
            let incorrect = decodedData.results?[0].incorrect_answers ?? ["error"]
            
            let question = QuizModel(correctAnswer: correct, question: quest, falseAnswer: incorrect)

         // print(question.correctAnswer)
        // print(question.question)
       // print(question.falseAnswer)
            
            return question
            

        } catch {
            print(error)
            return nil

            
        }
    }
}

QuizData

import Foundation


 // MARK: - Welcome
 struct Welcome: Codable {
 let results: [Result]?
 }
 
 // MARK: - Result
 struct Result: Codable {
     
 let category: String?
 let question, correct_answer: String?
 let incorrect_answers: [String]?
 }

QuizModel

import Foundation
struct QuizModel {
    
    let correctAnswer : String
    let question : String
    let falseAnswer : [String]
}

ViewController

import UIKit

class ViewController: UIViewController {
 
    
    @IBOutlet weak var ChoiceButton4: UIButton!
    @IBOutlet weak var ChoiceButton3: UIButton!
    @IBOutlet weak var ChoiceButton2: UIButton!
    @IBOutlet weak var ChoiceButton1: UIButton!
    @IBOutlet weak var QuestionTextView: UITextView!
    
    var quizMangager = QuizManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        QuestionTextView.layer.cornerRadius = 15
       
        quizMangager.performRequest()
        self.quizMangager.delegate = self
        
    }
    
}

extension ViewController : quizManagerDelegate{
    func didUpdateQuiz(_ Quizmanager: QuizManager, quiz: QuizModel) {
                DispatchQueue.main.async {
                   print("***")
            print(quiz.correctAnswer)
            
        }
    }
}

CodePudding user response:

The problem is in viewDidLoad that lines

 quizMangager.performRequest()
 self.quizMangager.delegate = self

You call performRequest before to delegate it so when performRequest is called var delegate: quizManagerDelegate? is nil . Just call delegate before call function

 override func viewDidLoad() {
    super.viewDidLoad()
    ...
    quizMangager.delegate = self
    quizMangager.performRequest()
    ...
}

CodePudding user response:

You have a slight mistake in your code. You just need to confirm the delegate first and then call its function .

  • Related