Book Image

SwiftUI Essentials - iOS Edition

By : Neil Smyth
Book Image

SwiftUI Essentials - iOS Edition

By: Neil Smyth

Overview of this book

Beginning with the basics, this book provides an outline of the steps necessary to set up an iOS development environment and introduced you to Swift Playgrounds so that you can learn and experiment with Swift. You’ll begin with an introduction to the key concepts of SwiftUI and its architecture. You’ll learn how to create customer SwiftUI views and combine them to create user interface layouts including how to use stacks, frames, and forms. As you progress, you’ll learn how to handle data using state properties. You’ll also learn how to use graphics, animation, transitions, and gestures in your user interface. The book finishes by explaining how to package an application and upload it to the App Store for publication. By the end of the book, you’ll have all the necessary skills to develop your own applications for iOS 13 with SwiftUI, Swift 5, and Xcode 11.
Table of Contents (373 chapters)
Free Chapter
1
1. Start Here. 1
2
1.1 For Swift Programmers 1
3
1.2 For Non-Swift Programmers. 2
4
1.3 Source Code Download. 2
5
1.4 Feedback. 2
6
1.5 Errata. 2
7
2. Joining the Apple Developer Program.. 3
8
2.1 Downloading Xcode 11 and the iOS 13 SDK. 3
9
2.2 Apple Developer Program.. 3
10
2.3 When to Enroll in the Apple Developer Program?. 4
11
2.4 Enrolling in the Apple Developer Program.. 4
12
2.5 Summary. 5
13
3. Installing Xcode 11 and the iOS 13 SDK. 7
14
3.1 Identifying Your macOS Version. 7
15
3.2 Installing Xcode 11 and the iOS 13 SDK. 8
16
3.3 Starting Xcode. 8
17
3.4 Adding Your Apple ID to the Xcode Preferences. 8
18
3.5 Developer and Distribution Signing Identities. 9
19
4. An Introduction to Xcode 11 Playgrounds. 11
20
4.1 What is a Playground?. 11
21
4.2 Creating a New Playground. 11
22
4.3 A Basic Swift Playground Example. 13
23
4.4 Viewing Results 15
24
4.5 Adding Rich Text Comments. 16
25
4.6 Working with Playground Pages 18
26
4.7 Working with UIKit in Playgrounds 18
27
4.8 Adding Resources to a Playground. 20
28
4.9 Working with Enhanced Live Views 21
29
4.10 Summary. 23
30
5. Swift Data Types, Constants and Variables. 25
31
5.1 Using a Swift Playground. 26
32
5.2 Swift Data Types 26
33
5.2.1 Integer Data Types 26
34
5.2.2 Floating Point Data Types. 27
35
5.2.3 Bool Data Type. 27
36
5.2.4 Character Data Type. 27
37
5.2.5 String Data Type. 28
38
5.2.6 Special Characters/Escape Sequences 29
39
5.3 Swift Variables 30
40
5.4 Swift Constants. 30
41
5.5 Declaring Constants and Variables 30
42
5.6 Type Annotations and Type Inference. 30
43
5.7 The Swift Tuple. 31
44
5.8 The Swift Optional Type. 32
45
5.9 Type Casting and Type Checking. 36
46
5.10 Summary. 38
47
6. Swift Operators and Expressions 39
48
6.1 Expression Syntax in Swift 39
49
6.2 The Basic Assignment Operator 39
50
6.3 Swift Arithmetic Operators 39
51
6.4 Compound Assignment Operators. 40
52
6.5 Comparison Operators 41
53
6.6 Boolean Logical Operators 42
54
6.7 Range Operators 42
55
6.8 The Ternary Operator 43
56
6.9 Bitwise Operators 44
57
6.9.1 Bitwise NOT. 44
58
6.9.2 Bitwise AND. 45
59
6.9.3 Bitwise OR. 45
60
6.9.4 Bitwise XOR. 45
61
6.9.5 Bitwise Left Shift 46
62
6.9.6 Bitwise Right Shift 46
63
6.10 Compound Bitwise Operators 47
64
6.11 Summary. 47
65
7. Swift Control Flow.. 49
66
7.1 Looping Control Flow.. 49
67
7.2 The Swift for-in Statement 49
68
7.2.1 The while Loop. 50
69
7.3 The repeat ... while loop. 51
70
7.4 Breaking from Loops 51
71
7.5 The continue Statement 52
72
7.6 Conditional Flow Control 52
73
7.7 Using the if Statement 53
74
7.8 Using if ... else … Statements 53
75
7.9 Using if ... else if ... Statements 54
76
7.10 The guard Statement 54
77
7.11 Summary. 55
78
8. The Swift Switch Statement 57
79
8.1 Why Use a switch Statement?. 57
80
8.2 Using the switch Statement Syntax. 57
81
8.3 A Swift switch Statement Example. 58
82
8.4 Combining case Statements. 58
83
8.5 Range Matching in a switch Statement 59
84
8.6 Using the where statement 60
85
8.7 Fallthrough. 60
86
8.8 Summary. 61
87
9. An Overview of Swift 5 Functions, Methods and Closures 63
88
9.1 What is a Function?. 63
89
9.2 What is a Method?. 63
90
9.3 How to Declare a Swift Function. 63
91
9.4 Implicit Returns from Single Expressions 64
92
9.5 Calling a Swift Function. 65
93
9.6 Handling Return Values 65
94
9.7 Local and External Parameter Names 65
95
9.8 Declaring Default Function Parameters 66
96
9.9 Returning Multiple Results from a Function. 67
97
9.10 Variable Numbers of Function Parameters. 67
98
9.11 Parameters as Variables 68
99
9.12 Working with In-Out Parameters 68
100
9.13 Functions as Parameters 69
101
9.14 Closure Expressions. 71
102
9.15 Closures in Swift 73
103
9.16 Summary. 73
104
10. The Basics of Object-Oriented Programming in Swift 75
105
10.1 What is an Instance?. 75
106
10.2 What is a Class?. 75
107
10.3 Declaring a Swift Class 75
108
10.4 Adding Instance Properties to a Class 76
109
10.5 Defining Methods 76
110
10.6 Declaring and Initializing a Class Instance. 78
111
10.7 Initializing and Deinitializing a Class Instance. 78
112
10.8 Calling Methods and Accessing Properties 79
113
10.9 Stored and Computed Properties. 80
114
10.10 Lazy Stored Properties 81
115
10.11 Using self in Swift 82
116
10.12 Understanding Swift Protocols 84
117
10.13 Opaque Return Types. 85
118
10.14 Summary. 86
119
11. An Introduction to Swift Subclassing and Extensions 87
120
11.1 Inheritance, Classes and Subclasses. 87
121
11.2 A Swift Inheritance Example. 87
122
11.3 Extending the Functionality of a Subclass 88
123
11.4 Overriding Inherited Methods 89
124
11.5 Initializing the Subclass 90
125
11.6 Using the SavingsAccount Class. 91
126
11.7 Swift Class Extensions 91
127
11.8 Summary. 92
128
12. An Introduction to Swift Structures. 93
129
12.1 An Overview of Swift Structures 93
130
12.2 Value Types vs. Reference Types 94
131
12.3 When to use Structures or Classes 96
132
12.4 Summary. 96
133
13. An Introduction to Swift Property Wrappers 97
134
13.1 Understanding Property Wrappers 97
135
13.2 A Simple Property Wrapper Example. 97
136
13.3 Supporting Multiple Variables and Types 99
137
13.4 Summary. 102
138
14. Working with Array and Dictionary Collections in Swift 103
139
14.1 Mutable and Immutable Collections 103
140
14.2 Swift Array Initialization. 103
141
14.3 Working with Arrays in Swift 104
142
14.3.1 Array Item Count 104
143
14.3.2 Accessing Array Items 105
144
14.4 Random Items and Shuffling. 105
145
14.5 Appending Items to an Array. 105
146
14.5.1 Inserting and Deleting Array Items. 105
147
14.6 Array Iteration. 106
148
14.7 Creating Mixed Type Arrays. 106
149
14.8 Swift Dictionary Collections 107
150
14.9 Swift Dictionary Initialization. 107
151
14.10 Sequence-based Dictionary Initialization. 108
152
14.11 Dictionary Item Count 108
153
14.12 Accessing and Updating Dictionary Items 109
154
14.13 Adding and Removing Dictionary Entries 109
155
14.14 Dictionary Iteration. 109
156
14.15 Summary. 110
157
15. Understanding Error Handling in Swift 5. 111
158
15.1 Understanding Error Handling. 111
159
15.2 Declaring Error Types 111
160
15.3 Throwing an Error 112
161
15.4 Calling Throwing Methods and Functions. 113
162
15.5 Accessing the Error Object 114
163
15.6 Disabling Error Catching. 114
164
15.7 Using the defer Statement 114
165
15.8 Summary. 115
166
16. An Overview of SwiftUI 117
167
16.1 UIKit and Interface Builder 117
168
16.2 SwiftUI Declarative Syntax 118
169
16.3 SwiftUI is Data Driven. 118
170
16.4 SwiftUI vs. UIKit 119
171
16.5 Summary. 120
172
17. Using Xcode in SwiftUI Mode. 121
173
17.1 Starting Xcode 11. 121
174
17.2 Creating a SwiftUI Project 122
175
17.3 Xcode in SwiftUI Mode. 122
176
17.4 The Preview Canvas 123
177
17.5 Preview Pinning. 125
178
17.6 Modifying the Design. 125
179
17.7 Editor Context Menu. 129
180
17.8 Previewing on Multiple Device Configurations 129
181
17.9 Running the App on a Simulator 131
182
17.10 Running the App on a Physical iOS Device. 132
183
17.11 Managing Devices and Simulators. 134
184
17.12 Enabling Network Testing. 135
185
17.13 Dealing with Build Errors. 135
186
17.14 Monitoring Application Performance. 135
187
17.15 Exploring the User Interface Layout Hierarchy. 136
188
17.16 Summary. 139
189
18. The Anatomy of a Basic SwiftUI Project 141
190
18.1 Creating an Example Project 141
191
18.2 UIKit and SwiftUI 141
192
18.3 The AppDelegate.swift File. 141
193
18.4 The SceneDelegate.swift File. 142
194
18.5 ContentView.swift File. 144
195
18.6 Assets.xcassets. 144
196
18.7 Info.plist 144
197
18.8 LaunchScreen.storyboard. 144
198
18.9 Summary. 144
199
19. Creating Custom Views with SwiftUI 145
200
19.1 SwiftUI Views. 145
201
19.2 Creating a Basic View.. 145
202
19.3 Adding Additional Views 146
203
19.4 Working with Subviews. 148
204
19.5 Views as Properties 149
205
19.6 Modifying Views 150
206
19.7 Working with Text Styles 150
207
19.8 Modifier Ordering. 152
208
19.9 Custom Modifiers 153
209
19.10 Basic Event Handling. 153
210
19.11 The onAppear and onDisappear Methods 154
211
19.12 Building Custom Container Views. 155
212
19.13 Summary. 156
213
20. SwiftUI Stacks and Frames 157
214
20.1 SwiftUI Stacks. 157
215
20.2 Spacers, Alignment and Padding. 159
216
20.3 Container Child Limit 161
217
20.4 Text Line Limits and Layout Priority. 162
218
20.5 SwiftUI Frames 164
219
20.6 Frames and the Geometry Reader 166
220
20.7 Summary. 167
221
21. Working with SwiftUI State, Observable and Environment Objects 169
222
21.1 State Properties 169
223
21.2 State Binding. 171
224
21.3 Observable Objects. 172
225
21.4 Environment Objects 174
226
21.5 Summary. 176
227
22. A SwiftUI Example Tutorial 177
228
22.1 Creating the Example Project 177
229
22.2 Reviewing the Project 178
230
22.3 Adding a VStack to the Layout 180
231
22.4 Adding a Slider View to the Stack. 180
232
22.5 Adding a State Property. 181
233
22.6 Adding Modifiers to the Text View.. 182
234
22.7 Adding Rotation and Animation. 183
235
22.8 Adding a TextField to the Stack. 185
236
22.9 Adding a Color Picker 185
237
22.10 Tidying the Layout 187
238
22.11 Summary. 190
239
23. SwiftUI Observable and Environment Objects – A Tutorial 191
240
23.1 About the ObservableDemo Project 191
241
23.2 Creating the Project 191
242
23.3 Adding the Observable Object 191
243
23.4 Designing the ContentView Layout 192
244
23.5 Adding the Second View.. 194
245
23.6 Adding Navigation. 195
246
23.7 Using an Environment Object 196
247
23.8 Summary. 198
248
24. SwiftUI Stack Alignment and Alignment Guides. 199
249
24.1 Container Alignment 199
250
24.2 Alignment Guides. 201
251
24.3 Using the Alignment Guides Tool 205
252
24.4 Custom Alignment Types 206
253
24.5 Cross Stack Alignment 209
254
24.6 ZStack Custom Alignment 212
255
24.7 Summary. 215
256
25. SwiftUI Lists and Navigation. 217
257
25.1 SwiftUI Lists 217
258
25.2 SwiftUI Dynamic Lists 219
259
25.3 SwiftUI NavigationView and NavigationLink. 221
260
25.4 Making the List Editable. 223
261
25.5 Summary. 226
262
26. A SwiftUI List and Navigation Tutorial 227
263
26.1 About the ListNavDemo Project 227
264
26.2 Creating the ListNavDemo Project 227
265
26.3 Preparing the Project 227
266
26.4 Adding the Car Structure. 228
267
26.5 Loading the JSON Data. 229
268
26.6 Adding the Data Store. 230
269
26.7 Designing the Content View.. 230
270
26.8 Designing the Detail View.. 233
271
26.9 Adding Navigation to the List 235
272
26.10 Designing the Add Car View.. 236
273
26.11 Implementing Add and Edit Buttons 239
274
26.12 Adding the Edit Button Methods. 241
275
26.13 Summary. 242
276
27. Building Tabbed Views in SwiftUI 243
277
27.1 An Overview of SwiftUI TabView.. 243
278
27.2 Creating the TabViewDemo App. 244
279
27.3 Adding the TabView Container 244
280
27.4 Adding the Content Views 244
281
27.5 Adding the Tab Items. 244
282
27.6 Adding Tab Item Tags. 245
283
27.7 Summary. 246
284
28. Building Context Menus in SwiftUI 247
285
28.1 Creating the ContextMenuDemo Project 247
286
28.2 Preparing the Content View.. 247
287
28.3 Adding the Context Menu. 248
288
28.4 Testing the Context Menu. 249
289
28.5 Summary. 250
290
29. Basic SwiftUI Graphics Drawing. 251
291
29.1 Creating the DrawDemo Project 251
292
29.2 SwiftUI Shapes 251
293
29.3 Using Overlays 254
294
29.4 Drawing Custom Paths and Shapes 254
295
29.5 Drawing Gradients. 257
296
29.6 Summary. 260
297
30. SwiftUI Animation and Transitions 261
298
30.1 Creating the AnimationDemo Example Project 261
299
30.2 Implicit Animation. 261
300
30.3 Repeating an Animation. 264
301
30.4 Explicit Animation. 264
302
30.5 Animation and State Bindings 265
303
30.6 Automatically Starting an Animation. 266
304
30.7 SwiftUI Transitions. 269
305
30.8 Combining Transitions 270
306
30.9 Asymmetrical Transitions 271
307
30.10 Summary. 271
308
31. Working with Gesture Recognizers in SwiftUI 273
309
31.1 Creating the GestureDemo Example Project 273
310
31.2 Basic Gestures. 273
311
31.3 The onChange Action Callback. 275
312
31.4 The updating Callback Action. 276
313
31.5 Composing Gestures 278
314
31.6 Summary. 280
315
32. Integrating UIViews with SwiftUI 281
316
32.1 SwiftUI and UIKit Integration. 281
317
32.2 Integrating UIViews into SwiftUI 282
318
32.3 Adding a Coordinator 283
319
32.4 Handling UIKit Delegation and Data Sources. 284
320
32.5 An Example Project 286
321
32.6 Wrapping the UIScrolledView. 286
322
32.7 Implementing the Coordinator 287
323
32.8 Using MyScrollView.. 288
324
32.9 Summary. 289
325
33. Integrating UIViewControllers with SwiftUI 291
326
33.1 UIViewControllers and SwiftUI 291
327
33.2 Creating the ViewControllerDemo project 291
328
33.3 Wrapping the UIImagePickerController 291
329
33.4 Designing the Content View.. 293
330
33.5 Completing MyImagePicker 294
331
33.6 Completing the Content View.. 296
332
33.7 Testing the App. 297
333
33.8 Summary. 297
334
34. Integrating SwiftUI with UIKit 299
335
34.1 An Overview of the Hosting Controller 299
336
34.2 A UIHostingController Example Project 300
337
34.3 Adding the SwiftUI Content View. 300
338
34.4 Preparing the Storyboard. 301
339
34.5 Adding a Hosting Controller 302
340
34.6 Configuring the Segue Action. 304
341
34.7 Embedding a Container View.. 306
342
34.8 Embedding SwiftUI in Code. 308
343
34.9 Summary. 310
344
35. Preparing and Submitting an iOS 13 Application to the App Store. 311
345
35.1 Verifying the iOS Distribution Certificate. 311
346
35.2 Adding App Icons 313
347
35.3 Designing the Launch Screen. 314
348
35.4 Assign the Project to a Team.. 314
349
35.5 Archiving the Application for Distribution. 315
350
35.6 Configuring the Application in iTunes Connect 316
351
35.7 Validating and Submitting the Application. 317
352
35.8 Configuring and Submitting the App for Review.. 320
353
An Introduction to Swift Playgrounds
354
Working with Array and Dictionary Collections in Swift
355
The Swift Switch Statement
356
Swift Control Flow
357
The Basics of Object-Oriented Programming in Swift
358
Creating Custom Views with SwiftUI
359
Integrating UIViews with SwiftUI
360
Installing Xcode 11 and the iOS 13 SDK
361
SwiftUI Stacks and Frames
362
Joining the Apple Developer Program
363
An Overview of Swift 5 Functions, Methods and Closures
364
SwiftUI Stack Alignment and Alignment Guides
365
A SwiftUI Example Tutorial
366
SwiftUI Observable and Environment Objects – A Tutorial
367
SwiftUI Lists and Navigation
368
The Anatomy of a Basic SwiftUI Project
369
SwiftUI Animation and Transitions
370
Working with SwiftUI State, Observable and Environment Objects
371
A SwiftUI List and Navigation Tutorial
372
Integrating UIViewControllers with SwiftUI
373
Integrating SwiftUI with UIKit
30.3 Repeating an Animation

By default, an animation will be performed once each time it is initiated. An animation may, however, be configured to repeat one or more times. In the following example, the animation is configured to repeat a specific number of times:

.animation(Animation.linear(duration: 1).repeatCount(10))

Each time an animation repeats, it will perform the animation in reverse as the view returns to its original state. If the view is required to instantly revert to its original appearance before repeating the animation, the autoreverses parameter must be set to false:

.animation(Animation.linear(duration: 1).repeatCount(10,

       autoreverses: false))

An animation may also be configured to repeat indefinitely using the repeatForever() modifier as follows:

.repeatForever(autoreverses: true))