NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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 Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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.