WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Help in creating data model and how to solve related SwiftUI issue

Forums > SwiftUI

Hey guys,

I'm in the middle of creating a data model for my app and need help in tackling an issue.

The setting:

  • SwiftUI app with main start grid view (select an "island") and navigation links to detail views (details for "island").
  • On the detail view there should be a horizontal scroll view filled "exercises" (which are different; some are only a view with a story, some are little games)
  • All of this should be based on json data looking like this:
 {
        "id": 1,
        "lektionenTitel": "Glück",
        "aufInselTitel": "Was bedeutet Glück für mich?",
        "einleitungsText": "Auf der Glücksinsel findest du heraus, was Glück für dich persönlich bedeutet und was dich alles so glücklich macht.",
        "textLesson": [
            {
                "id": 1,
                "title": "Die Geschichte der Geduld",
                "text": "ablanksajkldjasljdads"

            }
        ],
        "selectLesson": [
            {
                "id": 1,
                "items": ["happy", "sad"],
                "selectedItems": []
            }

        ]
    },

How do I put different "exercises" (textLesson, selectLesson) together into single horizontal scrollview?

phot of detail view and exercises scroll view

   

I didn't fully understand what you wanted to achieve with the decoded data, but simply looking at your json, this is how I would write your model

struct Model1: Codable {
    var id: Int
    var lektionenTitel: String
    var aufInselTitel: String
    var einleitungsText: String
    var textLsson: [Model2]
    var selectLesson: [Model3]

    struct Model2: Codable {
        var id: Int
        var title: String
        var text: String
    }

    struct Model3: Codable {
        var id: Int
        var title: [String]
        var selectedItems: [String]
    }
}

   

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.