Book Image

SwiftUI Projects

By : Craig Clayton
Book Image

SwiftUI Projects

By: Craig Clayton

Overview of this book

Released by Apple during WWDC 2019, SwiftUI provides an innovative and exceptionally simple way to build user interfaces for all Apple platforms with the power of Swift. This practical guide involves six real-world projects built from scratch, with two projects each for iPhone, iPad, and watchOS, built using Swift programming and Xcode. Starting with the basics of SwiftUI, you’ll gradually delve into building these projects. You’ll learn the fundamental concepts of SwiftUI by working with views, layouts, and dynamic types. This SwiftUI book will also help you get hands-on with declarative programming for building apps that can run on multiple platforms. Throughout the book, you’ll work on a chart app (watchOS), NBA draft app (watchOS), financial app (iPhone), Tesla form app (iPhone), sports news app (iPad), and shoe point-of-sale system (iPad), which will enable you to understand the core elements of a SwiftUI project. By the end of the book, you’ll have built fully functional projects for multiple platforms and gained the knowledge required to become a professional SwiftUI developer.
Table of Contents (13 chapters)

SwiftUI and Core Data optionals

If you have used Swift before, you are familiar with Optionals, but it is handled much differently when it comes to Core Data. If you uncheck the Optional checkbox in Attributes inspector, it will still be generated as an optional Swift property. Core Data only cares that properties have values when saved and can be nil at other times. There are different ways to handle this, but I found the most elegant way was to create an extension in the Optional class. If you look inside the Utilities folder, you will see three files — IntOptional.swift, BoolOptional.swift, and StringOptional.swift. Each of these files is structured the same way, but they work for Int, Boolean, and String, respectively.

Let's look at StringOptional.swift in a little more detail using the following code:

Figure 7.12

Now with this file, we can make our code a bit shorter:

  1. We do not have to do something like this in our code: