NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

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?

1      

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

1      

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

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

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.