0

I am facing issues setting up Binding in the following SwiftUI code snippet. I am trying this on xCode Beta 7 ((11M392r).

In the code snippet below, I am creating 2 Stepper views.

If I pass $student.totalMarks to Stepper, it works and creates the right Binding.

But if I try to access $student.marks.score1, that does not work and shows the following compilation error:

Generic parameter 'Subject' could not be inferred.

Is there a way to pass single field from a nested property into a binding?

struct Marks {
    public let score1: Int
    public let score2: Int
    public let score3: Int
}

class Student: ObservableObject {
    @Published var totalMarks: Int = 145
    @Published var marks = Marks(score1: 67, score2: 56, score3: 64)
}

struct ContentView: View {
    @ObservedObject var student = Student()

    var body: some View {
        return VStack {
            Stepper("Total Score: \(student.totalMarks)", value: $student.totalMarks)
            Stepper("Score 1: \(student.marks.score1)", value: $student.marks.score1)
        }
    }
}

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

1 Answer 1

1

Easy. ;-) Don't use a constant for a stepper's value binding. Rather make your scores variables (using var instead of let in struct Marks).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.