Book Image

Elevate SwiftUI Skills by Building Projects

By : Frahaan Hussain
Book Image

Elevate SwiftUI Skills by Building Projects

By: Frahaan Hussain

Overview of this book

Elevate SwiftUI Skills by Building Projects helps you harness the cutting-edge potential of SwiftUI and its innovative and user-friendly approach to crafting user interfaces for Apple platforms with the power of Swift. This book will enhance your UI programming skills with SwiftUI through a project-based methodology, guiding you to create four real-world projects. Starting with a quick recap of Swift and SwiftUI, you’ll gradually develop projects tailored for iPhone, iPad, macOS, and watchOS using Swift and Xcode. You’ll experience SwiftUI’s versatility in action as you build a tax calculator for iPhone and a photo gallery for the iPad, which uses a larger display to enhance the viewing experience. You’ll also create an app store for Mac and, finally, get to grips with the power of SwiftUI for smaller devices such as the Apple Watch by designing a Fitness Companion app. By the end of this book, you'll have built fully functional projects across multiple platforms and gained the expertise needed to excel as a professional SwiftUI developer.
Table of Contents (12 chapters)

Validating salary input

As of now, if you press the Calculate Tax button, it takes the user to the results page from the front page. However, it does this regardless of input, so it will go to the next page even if there is no salary. The following validation checks must be done for it to be an acceptable value:

  • Does it contain a value that is a number?
  • Is the value above 0 (this rules out negatives)?

You might be wondering why we can’t just check whether the salary is above 0 as we have chosen a decimal keypad. There are two main reasons for this:

  • The user can insert decimal points in a way that makes the input Not a Number (NaN) – for example, 4.5.6...
  • Even though the user cannot directly type text due to the keyboard being a decimal pad, they can still copy it from another application and paste it into our calculator, thus breaking the number-only TextField. You may think it’s worth just disabling pasting but it’s important...