UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

2      

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]
    }
}

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.