Book Image

Learn Swift by Building Applications

By : Emil Atanasov, Giordano Scalzo, Emil Atanasov
Book Image

Learn Swift by Building Applications

By: Emil Atanasov, Giordano Scalzo, Emil Atanasov

Overview of this book

Swift Language is now more powerful than ever; it has introduced new ways to solve old problems and has gone on to become one of the fastest growing popular languages. It is now a de-facto choice for iOS developers and it powers most of the newly released and popular apps. This practical guide will help you to begin your journey with Swift programming through learning how to build iOS apps. You will learn all about basic variables, if clauses, functions, loops, and other core concepts; then structures, classes, and inheritance will be discussed. Next, you’ll dive into developing a weather app that consumes data from the internet and presents information to the user. The final project is more complex, involving creating an Instagram like app that integrates different external libraries. The app also uses CocoaPods as its package dependency manager, to give you a cutting-edge tool to add to your skillset. By the end of the book, you will have learned how to model real-world apps in Swift.
Table of Contents (14 chapters)
5
Adding Interactivity to Your First App

Swift Basics – Variables and Functions

In this chapter, we will present the basics of the Swift language, starting from square one: introducing the basic concepts. The code, which is part of the chapter, illustrates the topics under discussion. In the next chapter, we will learn how to execute code samples in Xcode.

Let's begin with a brief history of Swift. This is a brand new programming language, developed by Apple and announced in 2014. In 2016, Swift 3 was released as open source, and this is the first major version, which enables people interested in Swift to develop the language. This means only one thing: Swift will start spreading even faster, beyond Apple's ecosystem. In this book, we will give examples of Swift, and we will discuss most of our solutions related to iOS, but you should know that the knowledge here is applicable across all places where Swift code is used.

Before diving into real code, let's define some basic concepts that we can use later in the book.

What is a computer program or application (app)? Simply, we can think of an app as a set of computer instructions that can be executed. Each app has a source code, written in a language describing all actions that the program does. In our case, we will write mobile (iOS) apps in Swift.

There are many low-level computer instructions, but Swift helps us to write without hassle, without knowing much about the low-level organization. Now we will start with the basic concept of variables.

We will discuss the following topics:

  • Constants and variables
  • Initializing using expressions
  • Basic types in Swift
  • Optional types
  • Enumeration types
  • Code flow statements – if, switch, loops
  • Functions
  • Tuples
  • The guard statement
  • Top-down and bottom-up

We begin with the basic building blocks of all programs.