Search icon CANCEL
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

eBook
€13.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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 feature icon AI Assistant (beta) to help accelerate your learning
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.

Who is this book for?

This SwiftUI book helps any mobile app developer looking to understand the fundamentals of the new SwiftUI framework along with the benefits of cross-device development. A solid understanding of iOS and macOS app development, along with some knowledge of the Swift programming language, will be beneficial. Basic programming knowledge is essential to grasp the concepts covered in the book effectively.

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
Estimated delivery fee Deliver to Latvia

Premium delivery 7 - 10 business days

€25.95
(Includes tracking information)

Product Details

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

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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 feature icon AI Assistant (beta) to help accelerate your learning
Estimated delivery fee Deliver to Latvia

Premium delivery 7 - 10 business days

€25.95
(Includes tracking information)

Product Details

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

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 96.97
SwiftUI Projects
€29.99
SwiftUI Cookbook
€41.99
Learn SwiftUI
€24.99
Total 96.97 Stars icon

Table of Contents

16 Chapters
Getting Started with SwiftUI Chevron down icon Chevron up icon
Understanding Declarative Syntax Chevron down icon Chevron up icon
Building Layout and Structure Chevron down icon Chevron up icon
Creating Your First Application Chevron down icon Chevron up icon
Understanding Controls, Views, and Lists Chevron down icon Chevron up icon
Working with Navigation in SwiftUI Chevron down icon Chevron up icon
Creating a Form with States and Data Binding Chevron down icon Chevron up icon
Networking and Linking to Your Existing App Logic Chevron down icon Chevron up icon
Maps and Location Services Chevron down icon Chevron up icon
Updating for iPad with NavigationViewStyle Chevron down icon Chevron up icon
SwiftUI on watchOS Chevron down icon Chevron up icon
SwiftUI versus UIKit Chevron down icon Chevron up icon
Basic Animation in Views Chevron down icon Chevron up icon
Animations in Transitions Chevron down icon Chevron up icon
Testing in SwiftUI Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.6
(5 Ratings)
5 star 20%
4 star 0%
3 star 20%
2 star 40%
1 star 20%
Amazon Customer Oct 03, 2022
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
Book started off with a great deal of potential, getting a few chapters in, the errors, mainly relating to the descriptive text being unrelated to the sample code with respect to object and variable names became increasingly apparent. I would like to see an update to this book with these issues addressed as it has the potential to be very good but should have been properly proof read before publication.
Amazon Verified review Amazon
Michael1260 May 25, 2021
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
Bof, un livre bourré d'erreurs de typos, des passages laissés sous silence. Je ne recommande pas ce livre.
Amazon Verified review Amazon
HELLO Sep 15, 2020
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
When I got to the end of chapter four I was very excited about the rest of the book. So far the explanations have been clear and all of the steps needed were included by the author. However, things started getting wonky with chapter 5. At first I thought it was my own errors, but it became clear there are some deficiencies in the later chapters. I've made it to chapter nine of fifteen and it has continued to be problematic. Steps are left out, code samples get repeated for no reason. This has devolved into frequent references to the coded "solutions", which just leads me to skip to the end of the chapter. At chapter four I would have rated this books as a five, but as I continue I can only rate it as average. UPDATE, by chapter 9 there were so many errors that it was pointless to follow the instructions and I gave up. Rating from 3 to 1. Again, started great, then it seems the author or editor ran out of energy to proof the book for errors. My advice, download the source coder from the publisher website and review that, the book mostly gets in the way.
Amazon Verified review Amazon
B. Shepard Aug 26, 2020
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
Full of typos and errors, it is also geared toward more knowledgeable Swift coders.
Amazon Verified review Amazon
Brady Murphy Aug 20, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Before I get into my review, I want to state that I was asked to read this book and give a 100% genuine review of the content within it. With that being said, I promise to be genuine and give you my honest feedback on the pedagogy and content of the material. I will state that I taught SwiftUI development at a very large university, so I have firsthand seen what does and doesn't work.PLEASE NOTE: This book goes over SwiftUI 1.0. Since the release of 2.0, there is a few teachings in this book that are outdated, but I believe at its core, the content is timeless.What I like:This book contains fantastic breadth to it and focuses on topics, like MVVM, that really need to be honed in early on as a developer get's into using SwiftUI. They cover everything from using maps, creating forms, using animations, etc. All of the essential "atoms" that are needed to create a decently complex iOS application are discussed in this book. I also really liked that they started off by having developers create a view in interface builder, which drives the elegance and beauty that SwiftUI presents to developers once they realize that they can create 10x the amount of work in nearly the same amount of code or less.I also think they do a great job with examples. I believe examples are the core of any teaching program and they must be efficient for the student to be able to get the example running fast and allow them to mess around to see how different code pieces affect the UI. In my opinion, they do exactly that. They provide code that can be copied and pasted fairly easily and then provide screenshots displaying the UI of how it should look on your screen.Lastly, I really appreciate that they encourage developers to start reading apples' documentation, and also provide outside resources for further reading in cases the reader would like to learn more depth about a specific chapter. Being able to read apples' documentation is vital for any developer, and they do a very good job of ingraining students to go look at protocols and types directly at the source before going to google or stack overflow.Things I maybe didn't like:I really didn't like that there wasn't a "challenge" at the end of chapters. For instance, some mini projects or assignments to test what you learned during that chapter. I think this is the one big missing piece of this book in my opinion. They do offer end of the chapter questions, but I believe small assignments where developers can test what they REALLY know and put their knowledge to practical use would be great.The second thing that I wish was more pertinent early on in the book, although I could just be a picky developer, is testing. It's the very last chapter in the book and I believe it needs to be talked about right after the MVVM architecture is introduced. Writing testable code with protocols and dependency injection is essential to writing clean and testable SwiftUI code. Sadly, most readers will look over this section since it's the last in the book and will not create a solid foundation early on.Overall:I would recommend buying this book and pairing it with a side project that you have in mind. This will allow you to add functionality to the project and really test what you know as you're learning it. I think they do a phenomenal job explaining the information, but also don't hold your hand too much. Congrats on the awesome book!
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela