Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Learn SwiftUI
Learn SwiftUI

Learn SwiftUI: An introductory guide to creating intuitive cross-platform user interfaces using Swift 5

Arrow left icon
Profile Icon Chris Barker
Arrow right icon
€13.98 €19.99
Book Apr 2020 316 pages 1st Edition
eBook
€13.98 €19.99
Print
€24.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Chris Barker
Arrow right icon
€13.98 €19.99
Book Apr 2020 316 pages 1st Edition
eBook
€13.98 €19.99
Print
€24.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€13.98 €19.99
Print
€24.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Learn SwiftUI

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:

  1. Search for Xcode in the App Store and select and download the latest version.
  2. Launch Xcode and follow any additional installation instructions that your system may prompt you for.
  3. 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:

“I would like a cup of tea, please"

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.

At the time of writing this book, Google has just announced Jetpack Compose for Android, which itself adopts the same approach to UI development.

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 {
...

Imperative syntax

Imperative syntax is the more common form of programming that's used as it's much more functional and requires the programmer to write code that will tell the compiler how we are going to achieve the goal, rather than ask politely. The following is an example of imperative syntax:

“I would like some boiled water, a teabag, milk, and sugar. Allow the tea to brew for n minutes then add n teaspoons of sugar and n amount of milk…… oh and remove the teabag."

Even with the preceding example, we could dig even deeper and say "I need some water, then boil the water" or "type or brand of sugar".

Let's take a look at the following Swift code. This is a typical class you may see when writing a standard Swift app. Take a closer look at the makeBrew() function and how each step is coded so that the compiler knows...

Summary

In this chapter, we learned how declarative syntax allows the developer to write syntax in a way that describes the actions and functions required and how its counterpart, imperative programming, is more logic-based.

We learned about the structure of declarative syntax and how understanding the view hierarchy is important to us, especially as every component on the screen is of the View type.

We also got to use SwiftUI for the first time in Xcode and took our first glance at the structure and arrangement that the declarative syntax has to offer, including nesting Views, and the importance of Group containers to arrange our subviews.

In the next chapter, we'll move onto the layout of SwiftUI and understand the structure of not just our code, but the architecture of our code base.

Questions

  1. Describe declarative syntax.
  2. What other syntax paradigms can we use?
  3. What do modifiers do?
  4. What visual effects do Groups have on our UI?
  5. When are we forced to use Groups in SwiftUI?
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Use SwiftUI for building dynamic apps for Apple devices from scratch
  • Understand declarative syntax in cross-platform development and how states work within SwiftUI
  • Learn to develop watchOS apps by reusing SwiftUI code

Description

SwiftUI is the new and powerful interface toolkit that lets you design and build iOS, iPadOS, and macOS apps using declarative syntax. It is a powerful way to develop the UI elements of applications, which would normally be tightly coupled to application logic. Learn SwiftUI will get you up to speed with the framework and cross-device UI development in no time. Complete with detailed explanations and practical examples, this easy-to-follow guide will teach you the fundamentals of the SwiftUI toolkit. You'll learn how to build a powerful iOS and iPadOS application that can be reused for deployment on watchOS. As you progress, you'll delve into UI and unit testing in iOS apps, along with learning how to test your SwiftUI code for multiple devices. The book will also show you how to integrate SwiftUI features such as data binding and network requests into your current application logic. By the end of this book, you will have learned how to build a cross-device application using the SwiftUI framework and Swift programming.

What you will learn

  • Explore the fundamentals of SwiftUI and compare it with existing UI frameworks
  • Write SwiftUI syntax and understand what should and shouldn t be included in SwiftUI s layer
  • Add text and images to a SwiftUI view and decorate them using SwiftUI s modifiers
  • Create basic forms, and use camera and photo library functions to add images to them
  • Understand the core concepts of Maps in iOS apps and add a MapView in SwiftUI
  • Design extensions within your existing apps to run them on watchOS
  • Handle networking calls in SwiftUI to retrieve data from external sources

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Apr 3, 2020
Length 316 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781839215421
Vendor :
Apple
Concepts :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Apr 3, 2020
Length 316 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781839215421
Vendor :
Apple
Concepts :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together

Stars icon
Total 52.96 76.97 24.01 saved
SwiftUI Projects
€15.99 €22.99
SwiftUI Cookbook
€22.99 €33.99
Learn SwiftUI
€13.98 €19.99
=
Book stack Total 52.96 76.97 24.01 saved Stars icon

Table of Contents

17 Chapters
Preface Chevron down icon Chevron up icon
1. Getting Started with SwiftUI Chevron down icon Chevron up icon
2. Understanding Declarative Syntax Chevron down icon Chevron up icon
3. Building Layout and Structure Chevron down icon Chevron up icon
4. Creating Your First Application Chevron down icon Chevron up icon
5. Understanding Controls, Views, and Lists Chevron down icon Chevron up icon
6. Working with Navigation in SwiftUI Chevron down icon Chevron up icon
7. Creating a Form with States and Data Binding Chevron down icon Chevron up icon
8. Networking and Linking to Your Existing App Logic Chevron down icon Chevron up icon
9. Maps and Location Services Chevron down icon Chevron up icon
10. Updating for iPad with NavigationViewStyle Chevron down icon Chevron up icon
11. SwiftUI on watchOS Chevron down icon Chevron up icon
12. SwiftUI versus UIKit Chevron down icon Chevron up icon
13. Basic Animation in Views Chevron down icon Chevron up icon
14. Animations in Transitions Chevron down icon Chevron up icon
15. Testing in SwiftUI Chevron down icon Chevron up icon
16. Other Books You May Enjoy Chevron down icon Chevron up icon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.