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

SOLVED: Day 35 - How do I pass the value from one struct to another

Forums > 100 Days of SwiftUI

How to pass the value qLeft from ContentView() to structQuestions and then use the actual answer variable in Play().

//
//  ContentView.swift
//  Tables
//
//  Created by Shaik Arham on 01/08/21.
//

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
    let questionRight = Int.random(in: 1...12)
    var actualAns = 0
    init(questionLeft: Int){

        actualAns = 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(questionLeft: 0)
    @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(qLeft: $qLeft.questionLeft))
                          .font(.system(size: 30, weight: .bold, design: .monospaced))
                            .foregroundColor(.white)
                    }

                    Spacer()
                }

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

struct Play: View {

    @Binding var qLeft: Int
    @State var questionLef = Questions(questionLeft: 0)
    var body: some View {
        VStack {
            Text("\(questionLef.questionLeft) * \(questionLef.questionRight)")

            Text("\(questionLef.actualAns)")
        }

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct Play_Previews: PreviewProvider {
    @State static var qLeft1 = 0
    static var previews: some View {
        Play(qLeft: $qLeft1)
    }
}

2      

Instead of:

    @Binding var qLeft: Int
    @State var questionLef = Questions(questionLeft: 0)

do this:

    @Binding var questionLef: Questions

And then you call Play like this:

Play(questionLef: $qLeft)

Although, unless you are going to be changing the properties of questionLef in Play, it doesn't need to be a @Binding; a simple let would suffice.

2      

Its working but why is the code below giving me errors 1. 'self' used before all stored properties are initialised and 2. Return from initializer withour initializing all stored properties.

struct Questions {
    @Binding var questionLeft: Int
    let questionRight = Int.random(in: 1...12)
    @State var actualAns = 0
    init(questionLeft: Int) {
        self.questionLeft = questionLeft
    }

    func calc(){
        actualAns = questionLeft * questionRight
    }

}

2      

Don't use @Binding and @State in your struct. Those are for use in Views, not just any old struct. Remove those property wrappers.

And you will then run into an issue with your calc function, since you are mutating the struct inside it. You will either need to do this:

    var actualAns = 0

    mutating func calc(){
        actualAns = questionLeft * questionRight
    }

or this:

    var actualAns: Int {
        questionLeft * questionRight
    }

    //and remove the calc function entirely

3      

Thank you a lot. Its working now.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.