How to turn a String to a Binding<String>

I'm sorta new to swiftUI, and I don't fully know how stuff like @Binding and @State work, but I have a view that changes a text from a variable passed to the view through:
Code Block
@Binding var str: String

A simple example of this would be:
Code Block
struct TextView: View {
  @Binding var str: String
  var body: some View {
    Text($str.wrappedValue)
  }
}
Though when I try to send a string when I call the view it gives me the error: Cannot convert the value of type 'String' to expected argument type 'Binding<String>

I have tried to figure out how to turn a String to a Binding<String> but I cant figure it out.
I do not know so much of SwiftUI but try to replace @Binding with @State
may be that could help.


when I try to send a string when I call the view

Please show the code.


By the way, $str.wrappedValue is equivalent to str, when str is an @Binding.


Here is an example of several ways to pass parameters:

Code Block
import SwiftUI
struct DestView: View {
var body: some View {
Text("Hello, Destination!")
}
}
struct DestViewState: View {
@State var theState: Int = 0
@State var theStateStr: String = "hello"
var body: some View {
Text("Hello, Destination! \(theState) \(theStateStr)")
}
}
struct DestViewStateBinding: View {
@Binding var theStateBinding: Int
@Binding var theStateStrBinding: String
var body: some View {
if theStateBinding < 50 { theStateBinding += 10 }
return Text("Hello, Destination! \(theStateBinding) \(theStateStrBinding)")
}
}
struct ContentView: View {
var contentNoState : Int = 1
@State var contentState : Int = 1
@State var contentStateStr: String = "hello you"
var body: some View {
NavigationView {
VStack {
Text("Hello, World!")
Spacer()
NavigationLink(destination: DestView()) {
Text("Press on me")
}.buttonStyle(PlainButtonStyle())
Spacer()
NavigationLink(destination: DestViewState(theState: contentNoState, theStateStr: "Modified hello")) {@// You can skip parameters as they are initialized in the struct
Text("Press on me with State")
}.buttonStyle(PlainButtonStyle())
Spacer()
NavigationLink(destination: DestViewStateBinding(theStateBinding: $contentState, theStateStrBinding: $contentStateStr)) {
Text("Press on me with Binding")
}.buttonStyle(PlainButtonStyle())
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

How to turn a String to a Binding&lt;String&gt;
 
 
Q