SwiUI Essentials
iOS 16 Edition
Figure 1-1Title
SwiUI Essentials – iOS 16 Edition
ISBN-13: 978-1-951442-52-1
© 2022 Neil Smyth / Payload Media, Inc. All Rights Reserved.
is book is provided for personal use only. Unauthorized use, reproduction and/or distribution strictly
prohibited. All rights reserved.
e content of this book is provided for informational purposes only. Neither the publisher nor the author oers
any warranties or representation, express or implied, with regard to the accuracy of information contained in
this book, nor do they accept any liability for any loss or damage arising from any errors or omissions.
is book contains trademarked terms that are used solely for editorial purposes and to the benet of the
respective trademark owner. e terms used within this book are not intended as infringement of any trademarks.
Rev: 1.0a
Figure 1-2Copyright
i
Contents
Table of Contents
1. Start Here .................................................................................................................................................... 1
1.1 For Swi Programmers ....................................................................................................................... 1
1.2 For Non-Swi Programmers ............................................................................................................. 1
1.3 Source Code Download ...................................................................................................................... 2
1.4 Feedback ............................................................................................................................................... 2
1.5 Errata..................................................................................................................................................... 2
2. Joining the Apple Developer Program ....................................................................................................... 3
2.1 Downloading Xcode 14 and the iOS 16 SDK .................................................................................. 3
2.2 Apple Developer Program .................................................................................................................. 3
2.3 When to Enroll in the Apple Developer Program? ......................................................................... 3
2.4 Enrolling in the Apple Developer Program ..................................................................................... 4
2.5 Summary .............................................................................................................................................. 5
3. Installing Xcode 14 and the iOS 16 SDK ................................................................................................... 7
3.1 Identifying Your macOS Version ...................................................................................................... 7
3.2 Installing Xcode 14 and the iOS 16 SDK .......................................................................................... 7
3.3 Starting Xcode ..................................................................................................................................... 8
3.4 Adding Your Apple ID to the Xcode Preferences............................................................................ 8
3.5 Developer and Distribution Signing Identities ............................................................................... 9
3.6 Summary .............................................................................................................................................. 9
4. An Introduction to Xcode 14 Playgrounds .............................................................................................. 11
4.1 What is a Playground? ...................................................................................................................... 11
4.2 Creating a New Playground ............................................................................................................. 11
4.3 A Swi Playground Example ........................................................................................................... 12
4.4 Viewing Results ................................................................................................................................. 14
4.5 Adding Rich Text Comments ..........................................................................................................16
4.6 Working with Playground Pages ..................................................................................................... 17
4.7 Working with SwiUI and Live View in Playgrounds ................................................................. 17
4.8 Summary ............................................................................................................................................ 20
5. Swi Data Types, Constants and Variables ............................................................................................. 21
5.1 Using a Swi Playground ................................................................................................................. 21
5.2 Swi Data Types ................................................................................................................................ 21
5.2.1 Integer Data Types ..................................................................................................................... 22
5.2.2 Floating Point Data Types ......................................................................................................... 22
5.2.3 Bool Data Type ........................................................................................................................... 23
5.2.4 Character Data Type .................................................................................................................. 23
5.2.5 String Data Type ......................................................................................................................... 23
5.2.6 Special Characters/Escape Sequences ..................................................................................... 24
5.3 Swi Variables .................................................................................................................................... 25
5.4 Swi Constants .................................................................................................................................. 25
5.5 Declaring Constants and Variables ................................................................................................. 25
5.6 Type Annotations and Type Inference ........................................................................................... 25
Contents
ii
Table of Contents
5.7 e Swi Tuple .................................................................................................................................. 26
5.8 e Swi Optional Type ................................................................................................................... 27
5.9 Type Casting and Type Checking.................................................................................................... 30
5.10 Summary .......................................................................................................................................... 32
6. Swi Operators and Expressions ............................................................................................................. 33
6.1 Expression Syntax in Swi ............................................................................................................... 33
6.2 e Basic Assignment Operator ...................................................................................................... 33
6.3 Swi Arithmetic Operators .............................................................................................................. 33
6.4 Compound Assignment Operators ................................................................................................. 34
6.5 Comparison Operators .....................................................................................................................34
6.6 Boolean Logical Operators............................................................................................................... 35
6.7 Range Operators ................................................................................................................................ 35
6.8 e Ternary Operator ....................................................................................................................... 36
6.9 Nil Coalescing Operator ................................................................................................................... 36
6.10 Bitwise Operators ............................................................................................................................ 37
6.10.1 Bitwise NOT .............................................................................................................................37
6.10.2 Bitwise AND ............................................................................................................................. 37
6.10.3 Bitwise OR ................................................................................................................................. 38
6.10.4 Bitwise XOR .............................................................................................................................. 38
6.10.5 Bitwise Le Shi ....................................................................................................................... 38
6.10.6 Bitwise Right Shi ....................................................................................................................39
6.11 Compound Bitwise Operators ....................................................................................................... 39
6.12 Summary .......................................................................................................................................... 40
7. Swi Control Flow .................................................................................................................................... 41
7.1 Looping Control Flow ...................................................................................................................... 41
7.2 e Swi for-in Statement ................................................................................................................ 41
7.2.1 e while Loop .......................................................................................................................... 42
7.3 e repeat ... while loop ................................................................................................................... 42
7.4 Breaking from Loops ........................................................................................................................ 43
7.5 e continue Statement ................................................................................................................... 43
7.6 Conditional Control Flow ................................................................................................................ 44
7.7 Using the if Statement ......................................................................................................................44
7.8 Using if ... else … Statements ......................................................................................................... 44
7.9 Using if ... else if ... Statements ........................................................................................................ 45
7.10 e guard Statement ....................................................................................................................... 45
7.11 Summary .......................................................................................................................................... 46
8. e Swi Switch Statement ...................................................................................................................... 47
8.1 Why Use a switch Statement? ......................................................................................................... 47
8.2 Using the switch Statement Syntax ................................................................................................ 47
8.3 A Swi switch Statement Example ................................................................................................. 47
8.4 Combining case Statements ............................................................................................................ 48
8.5 Range Matching in a switch Statement ........................................................................................... 49
8.6 Using the where statement ............................................................................................................... 49
8.7 Fallthrough ......................................................................................................................................... 50
8.8 Summary ............................................................................................................................................ 50
9. Swi Functions, Methods and Closures................................................................................................... 51