O'Reilly logo

Mastering iOS 11 Programming - Second Edition by Donny Wals

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using models for consistency

Adding a question model involves quite some refactoring. First, we must define the Question model. Create a new Swift file named Question and add the following implementation to it:

import Foundation

struct Question: Codable {
    
    enum CodingKeys: String, CodingKey {
        case title
        case answerA = "answer_a"
        case answerB = "answer_b"
        case answerC = "answer_c"
        case correctAnswer = "correct_answer"
    }
    
    let title: String
    let answerA: String
    let answerB: String
    let answerC: String
    let correctAnswer: Int
}

If you followed along with Chapter 10, Fetching and Displaying Data From the Network, this model should look somewhat familiar. The Question struct conforms to the Codable protocol. Since not all property names match the JSON ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required