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

SOLVED: I want to know what is wrong with the mentioned code.

Forums > 100 Days of SwiftUI

I am always getting an error.

import SwiftUI
struct navModifier: ViewModifier {
    func body(content: Content)-> some View {
        content
            .foregroundColor(.white)
            .font(.custom("Helvetica",size: 40) .weight(.bold))
            .shadow(color: .green, radius: 3, x: 2, y: 1)
            .padding(EdgeInsets(top: 10, leading: 10, bottom: 0, trailing: 0))
    }
}

extension View {
    func navMod() -> some View {
        self.modifier(navModifier())
    }
}

struct Questions {
    var questionLeft: Int

    init(questionLeft: Int){
        let questionRight = Int.random(in: 1...12)
        var actualAns: Int = questionLeft * questionRight
        self.questionLeft = questionLeft
       }
}

struct ContentView: View {
    @State private var noQ = ["1", "5", "10", "all"]
    @State private var userQ = 0
    @State var qLeft: Questions
    @State var passV = 1

    var body: some View {
        NavigationView {
            ZStack {
                LinearGradient(gradient: Gradient(colors: [Color.yellow, Color.red]), startPoint: .topTrailing, endPoint: .bottomTrailing)
                    .edgesIgnoringSafeArea(.all)

                VStack {
                    Text("Settings")
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .padding(EdgeInsets(top: -20, leading: 20, bottom: 0, trailing: 0))
                        .font(.system(size: 30, weight: .semibold, design: .rounded))

                    Section(header: Text("Which table you wanna practice?")) {
                        Picker("Select Table", selection: $qLeft.questionLeft) {
                            ForEach(1 ..< 13) {
                                Text("\($0) Table")
                            }

                        }
                        .padding(EdgeInsets(top: -50, leading: 0, bottom: 0, trailing: 0))
                    }
                    .padding(EdgeInsets(top: 20, leading: 10, bottom: 0, trailing: 0))
                    .frame(maxWidth: .infinity, alignment: .leading)

                    Section(header: Text("How many questions do you want to answer ?")) {
                        Picker(selection: $userQ, label: Text("Select Questions")){
                            ForEach(noQ, id: \.self){

                                Text($0)
                            }
                        }
                        .padding(EdgeInsets(top: -60, leading: 0, bottom: 0, trailing: 0))
                    }
                    .padding(EdgeInsets(top: 20, leading: 10, bottom: 0, trailing: 0))
                    .frame(maxWidth: .infinity, alignment: .leading)

                    Section {

                 //       NavigationLink("Click to Begin",destination: Play())
                           // .font(.system(size: 30, weight: .bold, design: .monospaced))
                         //   .foregroundColor(.white)
                    }

                    Spacer()
                }

            }.toolbar {
                ToolbarItemGroup(placement: .navigation){
                    Text("Tables App")
                        .navMod()
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
     let qLeft1: Questions
    static var previews: some View {
        ContentView(qLeft: Questions(questionLeft: Int()))
    }
}

2      

It helps if you tell us what error you are getting.

But here's what I see...

In your ContentView, you never initialize qLeft. You need to do:

@State var qLeft = Questions(questionLeft: 0)

Or whatever Int you want to pass in for the questionLeft parameter.

2      

Thank you the error i was getting was solved.

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.