-2

I need to fetch some quizzes for my application from the server. Unfortunately, it seems that URLSession.DataTask.shared is not working. How do I fix the problem?

This is for Swift 4.

import Foundation
import UIKit

class QuizService {
    let baseUrl = "https://iosquiz.herokuapp.com/api/quizzes"

    func fetchQuizzes(completion: @escaping (([Quiz]?) -> Void)) -> Void {
        if let url = URL(string: baseUrl) {
            let request = URLRequest(url: url)

            let dataTask = URLSession.shared.dataTask(with: request) { (data, response, error) in
                if let data = data {
                    do {
                        let json = try JSONSerialization.jsonObject(with: data, options: [])
                        if let resultsList = json as? [String: Any], let results = resultsList["quizzes"] as? [[String: Any]] {
                            let quizzes = results.map({ json -> Quiz? in
                                print(json)
                                if
                                    let title = json["title"] as? String,
                                    let id = json["id"] as? Int,
                                    let level = json["level"] as? Int,
                                    let description = json["description"] as? String,
                                    let category = json["category"] as? String,
                                    let questions = json["questions"] as? [String: Any],
                                    let imageUrl = json["image"] as? String {
                                    let  quiz=Quiz(id:id,title:title,descript:description,category:category,level:level,imageUrl:imageUrl,questions:questions)
                                    return quiz
                                } else {
                                    return nil
                                }
                            }).filter { $0 != nil } .map { $0! }
                            completion(quizzes)
                        } else {
                            completion(nil)
                        }
                    } catch {
                        completion(nil)
                    }
                } else {
                    completion(nil)
                }
            }
            dataTask.resume()
        } else {
            completion(nil)
        }

    }
}

My error is that field of quizzes are null, so my code is not working in my view controller.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
D.J.
  • 1

1 Answers1

0

I took a look at the response here https://iosquiz.herokuapp.com/api/quizzes.

The "questions" should be array of dictionaries instead of dictionary.

so it should works if you replace this line

let questions = json["questions"] as? [String: Any]

with this line

let questions = json["questions"] as? [[String: Any]]

BTW, I prefer to extract the logic of parsing json into another method to keep fetchQuizzes method simple.

Hope this helps!

Michael Wang
  • 9,583
  • 1
  • 19
  • 17