2

I am trying to pass an array to another view. I want to pass the array simulation to operating conditions view.

@State var simulation = [Any]()
                

I know you can not see all the code but below is where if a button is pressed to show the operating conditions view and pass in an array after the array is loaded with data I have checked to make sure Simulation array does have values in it before passing and it does.

.sheet(isPresented: $showingOperatingConditions) {
            OperatingConditionsView(threats: simulation)
        }

Here is the operating conditions view where threats is declared. For some reason, the array is empty every time I load the view. Can anybody help?

struct OperatingConditionsView: View {


    @State public var threats = [Any]()

}
5
  • Perhaps declare the simulation @ State and then define it in the sheet view as @ binding and then pass it with $simulation? Commented May 16, 2021 at 17:59
  • 1
    Change from @State to @Binding if you are going to change the array otherwise just remove @State. Off topic but why is the array declared as [Any]? Commented May 16, 2021 at 18:01
  • because the array is a json object @JoakimDanielson Commented May 16, 2021 at 18:05
  • @sheldor it is giving me errors when I change this.. I may be doing it wrong. Can you reply with a visual answer? Commented May 16, 2021 at 18:09
  • Don’t know if it is allowed here, but can I get an upvote for the reply please, because I really need this for the next step, thank you :) Commented May 16, 2021 at 20:22

1 Answer 1

2

As the comments said, you want to change OperatingConditionsView's threats into a @Binding. You'll then need to pass in $simulation with a $ (gets its Binding).

.sheet(isPresented: $showingOperatingConditions) {
    OperatingConditionsView(threats: $simulation) /// need a $ here
}
struct OperatingConditionsView: View {
    @Binding public var threats: [Any] /// don't initialize with default value
}

Alternatively, if you don't need changes in threats to auto-update simulation, you can go with a plain, non-binding property as @Joakim Danielson's comment said.

.sheet(isPresented: $showingOperatingConditions) {
    OperatingConditionsView(threats: simulation)
}
struct OperatingConditionsView: View {
    public var threats: [Any]
}
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.