Understanding Declarative Syntax
In this chapter, you'll learn about the core fundamentals of the declarative syntax. You'll be able to identify and understand the theory behind it and the part it plays in SwiftUI development. We'll look specifically at how the syntax is written and how easily it can be intercepted and edited in a live debugging environment, all within Xcode. We'll finish off by going deeper into the structure of the syntax and understand how it all binds together. By the end of this chapter, you'll be able to fully understand what advantages are on offer, along with how to comfortably write a basic UI using declarative syntax.
Without declarative syntax, there would be no SwiftUI, or it would simply be another UIKit forcing users to learn a new framework with the same base language. Due to this, it is important to learn the fundamentals...
Technical requirements
For this chapter, you'll need to download Xcode version 11.3 or above from the Apple Mac App Store. You'll also need to be running the latest version of macOS (Catalina or above). Simply follow these steps:
- Search for Xcode in the App Store and select and download the latest version.
- Launch Xcode and follow any additional installation instructions that your system may prompt you for.
- Once Xcode has been fully launched, you're ready to go!
What is declarative syntax?
In this section, you'll learn what declarative syntax is and what immediate benefit it offers for writing clean code. You'll also learn about its counterpart imperative syntax in order to gain understanding from both paradigms.
The declarative syntax is a programming paradigm that allows you to write code in a more formal and procedural way. In essence, the declarative syntax is a way of describing the code you want to write, without having to worry about how it's going to be implemented.
The following is an example of declarative syntax if it was said in spoken language:
This is more of a statement than written logic as we are asking for something rather than being concerned about how we are going to get it.
Let's take our first look at SwitUI syntax. Here, we have created a Text Label...
Visualizing declarative syntax
As we mentioned in the previous chapter, declarative syntax is used by many languages. A relatively recent framework, Google's Flutter, took on the declarative syntax approach and the wider developer community was immediately hooked. With this, it was only a matter of time before other frameworks started to follow.
Now, let's take our first steps into programming with SwiftUI. We'll start by getting to grips with Xcode, learn the basics of how to create a new project, and start to write our very first SwiftUI code!
Getting started with SwiftUI in Xcode
Nesting and decoration
Just from the examples in the previous section, you've seen the immediate benefit of not only SwiftUI but how it uses the declarative syntax to create Views and add them to your application.
In this section, we are going to dive a little deeper down the rabbit hole and look at how declarative syntax makes use of modifiers to decorate our Views and how to best handle adding multiple Views inside each other without getting into too much trouble.
Modifiers
Modifiers in SwiftUI are a simple yet effective way of rendering custom interactions and decoration. Let's take our previous example, add some basic modifiers, and see what we get:
struct ContentView: View {
var body: some View {
...