Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Faciliate access to properties of a binding
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Jan 4, 2024
1 parent 8911cc8 commit eea8428
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Sources/Adwaita/Model/Data Flow/Binding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
/// }
/// ```
@propertyWrapper
@dynamicMemberLookup
public struct Binding<Value> {

/// The value.
Expand All @@ -64,6 +65,17 @@ public struct Binding<Value> {
/// The closure for settings the value.
private let setValue: (Value) -> Void

/// Get a property of any content of a `Binding` as a `Binding`.
/// - Parameter dynamicMember: The path to the member.
/// - Returns: The binding.
public subscript<Subject>(dynamicMember keyPath: WritableKeyPath<Value, Subject>) -> Binding<Subject> {
.init {
wrappedValue[keyPath: keyPath]
} set: { newValue in
wrappedValue[keyPath: keyPath] = newValue
}
}

/// Initialize a property that is bound from a parent view.
/// - Parameters:
/// - get: The closure for getting the value.
Expand Down
7 changes: 7 additions & 0 deletions user-manual/Basics/CreatingViews.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ struct ChangeTextView: View {
}
```

If you have a more complex type and want to pass a property of the type as a binding,
you can just access the property on the binding.

```swift
HelloView(text: $complexType.text)
```

Whenever you modify a state property (directly or indirectly through bindings),
the user interface gets automatically updated to reflect that change.

Expand Down

0 comments on commit eea8428

Please sign in to comment.