Reader small image

You're reading from  Elevate SwiftUI Skills by Building Projects

Product typeBook
Published inSep 2023
Reading LevelN/a
PublisherPackt
ISBN-139781803242071
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Frahaan Hussain
Frahaan Hussain
author image
Frahaan Hussain

Frahaan Hussain is a 3 time published author with over 500,000 students enrolled on his courses online and more than 40,000 loyal YouTube followers. Before he started teaching programming online, Frahaan graduated top of his class with honours in Computer Games Programming from De Montfort University. After just 2 years, he was invited back to become module leader at his Alma Mater. While consulting with huge clients such as Google and Chukong, Frahaan continues to further the education of others and himself.
Read more about Frahaan Hussain

Right arrow

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:

https://github.com/PacktPublishing/Elevate-SwiftUI-Skills-by-Building-Projects

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)
  • Xcode...

Standards used

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?

It is important to write good code and good code isn’t just code that runs well but code that is easily maintainable and readable. Good code is an art form.

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 @SonarSystems.

Indentation

You should always indent your code to be aligned with other code in the same hierarchy level. Use real tabs instead of spaces for indenting code. You can see this in the...

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
  • Shapes
  • 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 this section, we will look at views and how we can implement them in SwiftUI. We will also take a look at combining these views.

Views are the fundamental building blocks of an application’s user interface. A view object renders content within its rectangle bounds and handles any interactions with that content.

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
  • Spacers:
    • ScrollViewReader
  • Grids:
    • PinnedScrollableViews

In the following sections, we will show you the source code and examples for each type of layout.

Note

If you would like further information, visit Apple’s documentation: https://developer.apple.com/documentation/uikit/view_layout.

What are lazy stacks?

Lazy stacks are views that arrange their children in a line that expands vertically, creating items only as needed.

SwiftUI provides two different types of lazy stacks, LazyVStack and LazyHStack...

Device Previews

One of the many hurdles we will overcome is the differences between the four main Apple product categories. These categories are as follows:

  • 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:

Mac

Figure 1.13 – Mac settings

Figure 1.13 – Mac settings

iPad

Figure 1.14 – iPad settings

Figure 1.14 – iPad settings

iPhone

Figure 1.15 – iPhone settings ...

Summary

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.

...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Elevate SwiftUI Skills by Building Projects
Published in: Sep 2023Publisher: PacktISBN-13: 9781803242071
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Frahaan Hussain

Frahaan Hussain is a 3 time published author with over 500,000 students enrolled on his courses online and more than 40,000 loyal YouTube followers. Before he started teaching programming online, Frahaan graduated top of his class with honours in Computer Games Programming from De Montfort University. After just 2 years, he was invited back to become module leader at his Alma Mater. While consulting with huge clients such as Google and Chukong, Frahaan continues to further the education of others and himself.
Read more about Frahaan Hussain