Skip to content

Commit 15e52ba

Browse files
authored
Update README.md
1 parent 29fc900 commit 15e52ba

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,42 @@
1-
# swiftui-test
1+
# Text mirroring in SwiftUI
2+
3+
(Xcode 13.4.1)
4+
5+
In this project we use SwiftUI properties wrappers to reflect a textfield's value in a separated view.
6+
7+
# Properties wrappers
8+
9+
> State is inevitable in any modern app, but with SwiftUI it’s important to remember that all of our views are simply functions of their state – we don’t change the views directly, but instead manipulate the state and let _that_ dictate the result.
10+
*(Paul Husdon, on [hackingwithswift](https://www.hackingwithswift.com/quick-start/swiftui/whats-the-difference-between-observedobject-state-and-environmentobject)*
11+
12+
Properties wrappers are one of the biggest features of SwiftUI. Some of them allows you to manage the state of your application.
13+
14+
- `@StateObject private var entry` in `swiftui_testApp.swift`
15+
> This creates and owns the property `entry` that we will place in the environment later on.
16+
17+
- `@Published var value: String` in `Entry.swift`
18+
> The @Published keyword tells to SwiftUI we want to notify every views that observe this property.
19+
20+
- `@EnvironmentObject var entry: Entry` in `Card.swift` and `Mirror.swift`
21+
> An @EnvironmentObject catch a property we previously placed in the environment (the `entry` in `swiftui_testApp.swift`). This way we will get notified every time the @Published value is updated.
22+
In `Card.swift` we write the value using text-binding `$entry.value` and in `Mirror.swift` we read the value.
23+
24+
Using those 3 wrappers, `Mirror.swift` gets notified every time the `entry` is updated by `Card.swift` so the text is automatically reflected.
25+
26+
# Different approach
27+
28+
We could also have used an `@ObservedObject` property (see a different implementation on [swift_ui-observed-object](https://github.com/emilien-io/swiftui-test/tree/swift_ui-observed-object) branch.
29+
30+
Here, we use an `@ObservedObject` instead of using an `@AppState` object. As we saw earlier, the difference between these two wrapper is ownership. `@AppState` owns the object and handles its lifetime while `@ObservedObject` — as its name suggests — observe the object without owning it directly.
31+
32+
> Note: While `@EnvironmentObject` can be catch everywhere in your application (assuming that it has been previously placed in the environment), when we use `@ObservedObject`, if we want to pass the object from view A to D, it has to be passed through each views.
33+
34+
# Few references
35+
36+
- https://www.swiftbysundell.com/articles/property-wrappers-in-swift/
37+
- https://www.hackingwithswift.com/quick-start/swiftui/whats-the-difference-between-observedobject-state-and-environmentobject
38+
- https://www.hackingwithswift.com/quick-start/swiftui/what-is-the-observedobject-property-wrapper
39+
- https://www.hackingwithswift.com/quick-start/swiftui/what-is-the-published-property-wrapper
40+
- https://www.hackingwithswift.com/quick-start/swiftui/what-is-the-state-property-wrapper
41+
- https://www.hackingwithswift.com/quick-start/swiftui/what-is-the-stateobject-property-wrapper
42+
- https://www.hackingwithswift.com/quick-start/swiftui/what-is-the-environmentobject-property-wrapper

0 commit comments

Comments
 (0)