Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import Foundation
- // MARK: - Math
- struct Math: Codable {
- let classes: [Class]?
- }
- // MARK: - Class
- struct Class: Codable {
- let calculus: [Calculus]?
- }
- // MARK: - Calculus
- struct Calculus: Codable {
- let question: String?
- let options: [String]?
- let answer: String?
- }
- func getMath() -> Math
- {
- var math: Math?
- var requestDone: Bool = false
- let url = URL(string: "https://mathquestioneditor.herokuapp.com")
- let request = NSMutableURLRequest(url:url!);
- let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
- if error == nil {
- math = try? JSONDecoder().decode(Math.self, from: data!)
- requestDone = true
- }
- else {
- print(error ?? "error within error")
- // return nil
- }
- }
- task.resume()
- while (true)
- {
- if (requestDone)
- {
- return math!
- }
- }
- }
- var json: Math! = getMath()
- for i in 0...(json!.classes![0].calculus!.count-1)
- {
- print(json!.classes![0].calculus![i].question!)
- print(json!.classes![0].calculus![i].options![0])
- print(json!.classes![0].calculus![i].options![1])
- print(json!.classes![0].calculus![i].options![2])
- print(json!.classes![0].calculus![i].options![3])
- print(json!.classes![0].calculus![i].answer!)
- print()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement