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      

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.