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

SOLVED: Timeout in a for loop

Forums > SwiftUI

I have this code:

import SwiftUI

var timers = """
{
    "customers": ["Example", "another"],

    "projects": [
        [
            "Website",
            "Example",
            "0:34:23"
        ],
        [
            "Logo",
            "another",
            "0:58:56"
        ],
        [
            "Big Project",
            "another",
            "5:23:56"
        ]
    ]
}
"""

struct DecodedJSON: Decodable {
    let customers: [String]
    let projects: [[String]]
}

struct Sidebar: View {

    @State private var customers: [String] = []
    @State private var projects: [[String]] = []

    var body: some View {
        NavigationView {
            List(customers, id: \.self) { customer in
                Section(header: Text(customer)) {
                    ForEach(projects, id: \.self) { project in
                        if project[1] == customer {
                            NavigationLink(destination: TimerDetailView()) {
                                Text(project[0])
                            }
                        }
                    }
                }
            }
            .listStyle(.sidebar)
            .navigationTitle("Übersicht")

            TimerDetailView()
        }
        .onAppear {
            loadJSON()
            /*print(customers)
            print(projects)*/
        }
    }

    func loadJSON() {
        //depending on where your JSON is coming from,
        //you should do the necessary set up steps
        guard let data = timers.data(using: .utf8) else {
            fatalError("Could not convert JSON string to data")
        }

        //here's where the actual decoding happens
        do {
            let decodedJSON = try JSONDecoder().decode(DecodedJSON.self, from: data)
            customers = decodedJSON.customers
            projects = decodedJSON.projects
        }
        catch {
            print(error)
        }
    }

}

The endresult should look like this:

But instead the compiler has a timeout. Im using Swift Playgrounds. I dont know if this is the problem. Can somebody try this in XCode and tell me whether it works or not? Can someone help me if it does not work?

3      

Frohes neues Jahr! 2022

Ja! This works fine in the Macintosh version of Playgrounds.

I see the same screen that you posted. I added:

// Test structure
struct TimerDetailView : View {
    var body: some View {
        Text("Hallo Servus!")  // Test view
    }
}

And when I tap on Website, Logo, or Big Project the screen transitions to this TimerDetailView nicely.

Viel Glück

3      

that means its an playgrounds issue. thanks. and also happy new year

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.