0

How do I access a @State variable from a function that's inside a class that is inside the struct. The pseudocode looks like:

struct StructName: UIViewRepresentable {
    @State var variableName = value
    class className: NSObject {
        func functionName(parameters) {
            //How do I access variableName from here?
        }
    }
}

2 Answers 2

1

Use @Binding, and pass it as an argument on init:

struct StructName: UIViewRepresentable {

    @Binding var variableName: Bool

    func makeUIView(context: Context) -> UIView {
        return UIView()
    }

    func updateUIView(_ uiView: UIView, context: Context) {

    }

    func makeCoordinator() -> Coordinator {
        Coordinator(variableName: self.$variableName)
    }

     class Coordinator: NSObject {

        @Binding var variableName: Bool

        init(variableName: Binding<Bool>) {
           _variableName = variableName
        }

        func functionName() {
            //access variableName here
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I have an error on the line where I define the binding variable for the first time (inside the struct but not the class). The error given on that line is "Argument labels '(wrappedValue:)' does not match any available overloads. I set Type equal to Bool and value equal to false. There is not error on the second binding variable (I wasn't sure whether to keep the variable type as 'String' or change it to 'Bool' (either way worked). Then the next error is on the line where I set _varName = varName. It says "Cannot assign value of type 'String' to type 'Binding<String>'. Thoughts?
I'm sorry, I will update the answer with a working example.
Thank you, it is super close to working! When I initialize a map into ContentView, what is the argument that I give to the variableName? i.e. when I write MapView(variableName: VALUE) in ContentView, what should I write in place of value? The error I am getting says that it needs a value of type Binding<Bool> but I'm not sure what to enter?
VALUE must me another @Bindingvariable or an @State. If you choose the second option you must prepend a $ on MapView instantiation. `MapView(variableName: $contentViewStateVar). developer.apple.com/documentation/swiftui/binding
1

Here is possible approach. Concrete types are used for demo purpose.

struct StructName: UIViewRepresentable {
    @State var variableName: Bool = false

    class ClassName: NSObject {
        let boundVariable: Binding<Bool>

        init(variableName: Binding<Bool>) {
            self.boundVariable = variableName
        }

        func functionName(parameters: [String]) {
            boundVariable.wrappedValue = true
        }
    }

    // somewhere in place of ClassName instantiation below use
    // ClassName(boundVariable: $variableName)
}

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.