Swift and SwiftUI Recap
Firstly, I would like to thank you for reading my book, be it bought or borrowed, or whether you’re having a sneak peek in the Amazon preview, I thank you.
This chapter will recap Swift and SwiftUI. We will first cover the coding standards used throughout the book for our upcoming projects and the history of Swift and SwiftUI. Then, we will take a look at the requirements for going through the projects in this book. Coding standards can be very polarizing for programmers, but they really shouldn’t be. If there are any you disagree with, feel free to tweet me at
@SonarSystems and let me know why. But don’t let that detract from the book and what you can get from it.
Afterward, we will look at some specific SwiftUI code examples along with previews to close off the recap. We will look at how we can use views and controls; these are the visual building blocks of your application’s user interface. We will use them throughout the...
Technical requirements and standards
This book requires you to download Xcode version 14 or above from Apple’s App Store.
To install Xcode, just search for
Xcode in the App Store and select and download the latest version. Open Xcode and follow any additional installation instructions. Once Xcode has opened and launched, you’re ready to go.
Version 14 of Xcode has the following features/requirements:
- Includes SDKs for iOS 16, iPadOS 16, macOS 12.3, tvOS 16, and watchOS 9.
- Supports on-device debugging in iOS 11 or later, tvOS 11 or later, and watchOS 4 or later.
- Requires a Mac running macOS Monterey 12.5 or later.
Download the sample code from the following GitHub link:
Here are the hardware requirements:
- You need an Intel or Apple Silicon Mac
- 4GB RAM or more
Here are the software requirements:
- macOS 11.3 (Big Sur or later)
In this section, we will look at the coding standards that are used throughout this book. It is important to have consistent standards and know what the standards are.
Why do we need coding standards?
In the following sections, we will go through a set of standards that are used in the Swift programming language and these will be used throughout this book. If you do not fully agree with the standards, that is fine, but I felt it important to list the standards used in case you come across something you have never seen before, such as Yoda conditions – do any of you use them? If so, tweet me at
What is Swift?
In this section, we will cover what Swift is, its history, and how it works on a macro level. If you are an expert and just want to read about SwiftUI, feel free to skip this section.
Swift is a programming language created by Apple and the open source community. It is a general-purpose, compiled, and multi-paradigm programming language. It was released in 2014 as a replacement for Apple’s previous language, Objective-C. Due to the fact that Objective-C had remained virtually the same since the early 1980s, it was missing many features that modern languages have. Hence, the creation of Swift began; it has taken the Apple developer ecosystem by storm and is a hugely popular programming language. It is demanded by companies all over the world with excellent remuneration offered to those that know how to leverage its immense features. According to the PYPL index seen in the following figure, Swift is in the top 10 most popular languages, making it a must-have...
What is SwiftUI?
In this section, we will cover what SwiftUI is and the features provided that we will leverage throughout this book to create our projects. If you feel comfortable with SwiftUI and just want to see projects, then feel free to skip the remainder of this chapter.
SwiftUI is a user interface framework built on top of the Swift programming language. It provides many components for creating your app’s user interface; the following is a macro list of these components:
- Views and controls
- Layout containers
- Collection containers
- Presentation containers
In addition to the components in the preceding list, SwiftUI provides us with event handlers, allowing our apps to react to taps, gestures, and all other types of input they may receive from the user. The framework provides tools to manage the flow of data from the models to the views and controls that the end user interacts with.
Now we will look at the different core features...
Understanding and implementing views
In the following sections, we will show the source code and examples for each type of view. If you would like further information, visit Apple’s documentation at https://developer.apple.com/documentation/uikit/views_and_controls.
What are text views?
It is very common to need to display text in our app, and we do this by using a text view, which draws a string. By default, it has a font assigned to it that is best for the platform it is being displayed on; however, you can change the font using the
font(_:) view modifier.
The following snippet shows the code used to implement a text view:
var body: some...
Understanding and implementing layouts
This section will cover how we can arrange our views using layouts for a more dynamic user experience.
SwiftUI layouts allow us as developers to arrange views in your app’s interface using the layout tools provided. Layouts tell SwiftUI how to place a set of views, and how much space it needs to do so to provide the desired layout.
Layouts can be but are not limited to any of the following:
- Lazy stacks
In the following sections, we will show you the source code and examples for each type of layout.
If you would like further information, visit Apple’s documentation: https://developer.apple.com/documentation/uikit/view_layout.
What are lazy stacks?
SwiftUI provides two different types of lazy stacks,
- Mac – iMac, Mac Pro, MacBook, anything that runs macOS, even Hackintosh
- iPad – Mini, regular, Air, Pro, all of them
- iPhone – Mini, Pro, Pro Max, all iPhones
- Apple Watch – small, big, old, or new
The obvious thing you will notice is that they get smaller and smaller. They naturally have different purposes; an Apple Watch won’t replace a Mac and vice versa. That is why the next eight chapters are grouped into pairs, one for each product category. We will uncover the design decisions and restrictions we have when creating our applications. Let’s take a look at the settings in each product category:
Figure 1.13 – Mac settings
Figure 1.14 – iPad settings
In this chapter, we covered the history of Swift and SwiftUI, the features provided to us on a macro level, and how Swift and SwiftUI work on a technical level. Then, we looked at the difference between Swift and SwiftUI and what features are offered, along with code samples for you to take away and use in your own projects. After that, we took a look at the requirements necessary for developing applications with Swift and SwiftUI. Then, we took a look at the coding standards that are used throughout this book, providing a reference point for any coding style that is unfamiliar to your own. Then, we looked at the views and controls provided by SwiftUI for creating our own user experiences, including custom views by combining the fundamentals. Finally, we took a look at how we can organize these views using layouts and checked the device previews too.
In our next chapter, we’ll take a look at designing our first project, the tax calculator app that we will create....