private var x: Bool = false
let y: String = "Hello World"
@State private var showModal: Bool = false // For primitive data types
print(showModal) // -> false
print($showModal) // -> false (as Binding)
print(_showModal) // -> false (to initialize Value) Does not re-render when changed
@StateObject private var myObject: myObjectClass = myObjectClass() // for objects
Bindings in Swift allow a child function or a child view to read and modify a variable defined in a parent view. By using a binding, changes made to the variable in the child view are reflected in the parent view.
To indicate that a structure or class needs a binding to a variable, the @Binding
property wrapper declaration is used. For example:
@Binding var isShown: Bool
@ObservedObject var myObject: myObjectClass
This declaration means that isShown is a binding to a Bool variable and myObject is a binding to myObjectClass which is passed from the parent view.
A binding is typically passed from a parent view to a child view. In the parent view, a @State
variable is used to manage the state:
@State private var isShown: Bool = false
@StateObject private var myObject: myObjectClass = myObjectClass()
Then, a binding to this State
variable is passed to the child view:
ChildView(isShown: $isShown)
ChildView(myObject: $myObject)
Here, $isShown
is a binding to the @State
variable isShown
.
- A source of truth has to be private!
- A
@Binding
can only be bound to a@State
variable or another source of truth (like@ObservedObject
or@EnvironmentObject
). - If a variable is declared as a
@Binding
, it expects a binding as input. Passing a regular value directly (without the$
prefix) will result in a compilation error.
- Leightweight
- Performant
- Value Type
- Inheritance
- Reference Type
- public
- internal
- fileprivate
- private
To describe the type of a variable, use :
.
private var x: String = "Hello World!"
To describe the return type of a method or function, use ->
.
private var foo: (String, Int) -> Void
public var bar: () -> Int
func foobar() -> Int {
return 3
}