-
Notifications
You must be signed in to change notification settings - Fork 3
/
11-IfLet.swift
48 lines (43 loc) · 1.12 KB
/
11-IfLet.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import SwiftUI
import SwiftUINavigation
private let readMe = """
This demonstrates how to unwrap a binding of an optional into a binding of an honest value.
Tap the "Edit" button to put the form into edit mode. Then you can make changes to the message \
and either commit the changes by tapping "Save", or discard the changes by tapping "Discard".
"""
struct IfLetCaseStudy: View {
@State var string: String = "Hello"
@State var editableString: String?
var body: some View {
Form {
Section {
Text(readMe)
}
Binding(unwrapping: $editableString).map { $string in
VStack {
TextField("Edit string", text: $string)
HStack {
Button("Discard") {
editableString = nil
}
Spacer()
Button("Save") {
string = string
editableString = nil
}
}
}
}
if editableString == nil {
Text("\(string)")
Button("Edit") {
editableString = string
}
}
}
.buttonStyle(.borderless)
}
}
#Preview {
IfLetCaseStudy()
}