Passing Data Between views and changing the data

Im trying to update value inside main view but I’m not able to.Nor I’m I able to update it in the subview.whats the problem.Im getting the error type cannot confirm to view.

//Main View

struct ContentView: View {

@State var value:Int

var body: some View {

    VStack {
      
    value = value + 10
      Text(String(value))
    
    
    }
    .padding()
}

}

#Preview {
ContentView(value: 5)
}

//Sub View

struct SubView: View {

var body: some View {
    Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
    
     

    
    ContentView(value: 78)
    
}

}

#Preview {
SubView()
}

@AmitG2

Welcome to the community.

You can’t have any code inside the body of a View that does not return a View.

Since you have a VStack (which is a View) in which to have a Text (which is a View) you cannot have value = value + 10 since that does not return a View.

In the case of your code, the solution is to have a Button that you tap where you can have Button action code that performs the calculation.

For example:

struct ContentView: View {

    @State var value:Int = 0

    var body: some View {

        VStack {
            Text(String(value))
            Button("Increment vaue") {
                // Action closure to perform the calculation
                value = value + 10
            }
        }
        .padding()
    }
}

If you want to have a sub view in which you want to perform the calculation then you have to pass the value to the SubView and declare it as a Binding in the SubView. For example:

struct SubView: View {
    @Binding var value: Int
    
    var body: some View {
        Button("Increment vaue") {
            // Action closure to perform the calculation
            value = value + 10
        }
    }
}

Then in your ContentView change the code to this:

struct ContentView: View {

    @State var value:Int = 0

    var body: some View {

        VStack {
            Text(String(value))
            SubView(value: $value)
        }
        .padding()
    }
}

If this is all rather confusing then I would assume that you are new to SwiftUI and the question then arrises as to what courses you are following at this point?

Thanks Chris , this helped me in my journal app.Im currently following the foundations(2023) course.
Also ill need to take a break of 2-3 months from app development.How can I revise after I come back.I don’t want to watch all the videos again.

To be honest there is no easy way. Learning is reinforced by writing code over and over so you may find yourself referring back to previous projects anyway because that’s normal for just about everyone. I’m always referring back to old projects to refresh my memory on how I did something a while back.

Ok Thanks Chris
Also where do I move forward from Code With Chris meaning what after cwc+

Since you have not finished the courses on offer from CodeWithChris then you are kind of getting a bit ahead of yourself asking what’s after CWC.