Maxime Defauw Learning Swift€¦ · In this course you’ll learn the brand-new Swift language....

14
Maxime Defauw Learning Swift SAMPLE CHAPTERS

Transcript of Maxime Defauw Learning Swift€¦ · In this course you’ll learn the brand-new Swift language....

Page 1: Maxime Defauw Learning Swift€¦ · In this course you’ll learn the brand-new Swift language. Apple introduced it at the WWDC 2014, which is an abbreviation for the Apple Worldwide

Maxime Defauw

Learning Swift SAMPLE CHAPTERS

Page 2: Maxime Defauw Learning Swift€¦ · In this course you’ll learn the brand-new Swift language. Apple introduced it at the WWDC 2014, which is an abbreviation for the Apple Worldwide

Introduction1

“Begin at the beginning,” the King said, very gravely, “and go on till you come to the end: then stop. ”

– Lewis Carroll, Alice in Wonderland

SAMPLE CHAPTERS — Learning Swift by Maxime Defauw

Page 3: Maxime Defauw Learning Swift€¦ · In this course you’ll learn the brand-new Swift language. Apple introduced it at the WWDC 2014, which is an abbreviation for the Apple Worldwide

Hi and welcome to this Swift book!

In this course you’ll learn the brand-new Swift language. Apple introduced it at the WWDC 2014, which is an abbreviation for the Apple Worldwide Developer Conference, an annual conference where Apple showcases its new products. This time, Apple surprised us all by introducing two unexpected products: the Apple Watch and Swift, a brand-new program-ming language that has a lot of new features and capabilities.

But what is a programming language?

Computers don’t understand English, French or whatever language that we, the people, speak. A programming language is kind of a special language programmers it use to ‘com-municate’ with a computer. Programs are in fact a set of instructions which a computer has to follow

There are a lot of different types of programming languages, but I won’t bother you here with the explanation of the differences between them. The best way to learn a foreign lan-guage is by actually speaking it. This is the same for programming languages! So, what are you waiting for? Let’s start!

Maxime Defauw

iiSAMPLE CHAPTERS — Learning Swift by Maxime Defauw

Page 4: Maxime Defauw Learning Swift€¦ · In this course you’ll learn the brand-new Swift language. Apple introduced it at the WWDC 2014, which is an abbreviation for the Apple Worldwide

To Françoise ‘Granny’ Mattelaer

Thanks for helping me writing my book. And for the delicious lunches every Monday.

iiiSAMPLE CHAPTERS — Learning Swift by Maxime Defauw

Page 5: Maxime Defauw Learning Swift€¦ · In this course you’ll learn the brand-new Swift language. Apple introduced it at the WWDC 2014, which is an abbreviation for the Apple Worldwide

Installing Xcode2

“The beginning is always to-day.”

– Mary Wollstonecraft

SAMPLE CHAPTERS — Learning Swift by Maxime Defauw

Page 6: Maxime Defauw Learning Swift€¦ · In this course you’ll learn the brand-new Swift language. Apple introduced it at the WWDC 2014, which is an abbreviation for the Apple Worldwide

Let’s begin by installing Xcode. Xcode is an IDE or Integrated Development Environment. It is a set of development tools used to create IOS and Mac OS X applications. Xcode 6, the latest version of Xcode at the time of writing, is the one we will use in this book. You will need to install Xcode on your Mac in order to complete the tutorials of this book . Of course, it is completely free!

You can download Xcode on the Apple Developer site or on the Mac App Store. (Fig. 2-1) You can find the Mac App Store in your Applications folder.

�Xcode on the Mac App Store. Click the ‘Get’ button under the Xcode logo. (Fig 2-1)

5SAMPLE CHAPTERS — Learning Swift by Maxime Defauw

Page 7: Maxime Defauw Learning Swift€¦ · In this course you’ll learn the brand-new Swift language. Apple introduced it at the WWDC 2014, which is an abbreviation for the Apple Worldwide

Once Xcode is downloaded and installed, open it up. In most cases, Xcode will be down-loaded in your Applications Folder. If not, you could search for it with spotlight (The magni-fying glass at the top right corner of your mac) or Finder You will be greeted by this win-dow (Fig. 2-2)

You have different options presented in the launching screen. A new feature that comes with Swift is Playgrounds. A Playground is an interactive environment that allows you to try out new code and ideas and see the results in real time instead of having to create a real project. This sounds good for learning! Let’s create one! Click on ‘Get started with a play-ground’. This brings up a new window. (Fig. 2-3)

� Greeting window when opening Xcode (Fig. 2-2)

6SAMPLE CHAPTERS — Learning Swift by Maxime Defauw

Page 8: Maxime Defauw Learning Swift€¦ · In this course you’ll learn the brand-new Swift language. Apple introduced it at the WWDC 2014, which is an abbreviation for the Apple Worldwide

Choose a name for your playground. I’ll just leave it with ‘MyPlayground’. Set ‘iOS’ for plat-form and click ‘Next’. Select a place where you want your playground to be saved and click ‘Create’. Xcode automatically opens our Playground file. This is where you will write your first lines of code!

As you can see, there’s already some code generated for you. It looks strange, right? In the next chapter, we will analyze this line-by-line and write our first lines of code!

7SAMPLE CHAPTERS — Learning Swift by Maxime Defauw

� Create your playground (Fig. 2-3)

Page 9: Maxime Defauw Learning Swift€¦ · In this course you’ll learn the brand-new Swift language. Apple introduced it at the WWDC 2014, which is an abbreviation for the Apple Worldwide

Types3

“The first step is you have to say that you can.”

– Will Smith

SAMPLE CHAPTERS — Learning Swift by Maxime Defauw

Page 10: Maxime Defauw Learning Swift€¦ · In this course you’ll learn the brand-new Swift language. Apple introduced it at the WWDC 2014, which is an abbreviation for the Apple Worldwide

Comments

Let’s start where we left in the last chapter. If it isn’t already, open up the playground we pre-viously created. When you make a playground file, there is by default some code gener-ated. What does it all mean? Let’s take a closer look.

// Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"

The first line is what is called ‘a comment’. It is just a little explanation the coder or the de-veloper wrote to make things a little easier. You can write everything you want in a com-ment. For example ‘this is a piece of code that calculates three minus five’ or ‘I left here yes-terday, and I still have to add a calculator code’. This is extremely useful when you work in team. For instance you could write something about the piece of code you made, and send it directly to your team member. Or if you have made a little program, and want to adapt it some weeks later, you could have written a comment about your code so you’ll still under-stand what you wrote some weeks later.

In Swift, comments can be made in two different ways. The first way is by typing two slashes followed by your comment. The second way, which is often used for longer com-ment sections, is by ‘opening’ the comment section with a /* and closing it with a */.

// This is a single line comment

/* This is a longer comment section.Here, you can type longer comments that consist of one or more lines*/

Try it out! Type something about this book in your playground. It is very good and educa-tional to code with me. So try out everything you learn in this book inside your playground. This way, you will understand and memorize everything better.

9SAMPLE CHAPTERS — Learning Swift by Maxime Defauw

Page 11: Maxime Defauw Learning Swift€¦ · In this course you’ll learn the brand-new Swift language. Apple introduced it at the WWDC 2014, which is an abbreviation for the Apple Worldwide

Variables

Before introducing you to the second line of code, I want to explain how a program works. When you write code, you decide what you want the computer to do by making instructions it has to follow. Next you run the instructions through the compiler, which is yet another pro-gram that converts your instructions into code your computer can understand. Now the ma-chine can execute your application.

The second line ‘import UIKit’ tells the compiler to include a certain file of framework in your project (in this case the UIKit framework). UIKit provides the crucial infrastructure needed to construct and manage IOS apps. Frameworks represent big chunks of code you won’t have to rewrite every time you make a new project. For example, the UIKit provides support for handling touch- and motion-based events on your iPhone, iPad or iPod Touch. Without this framework, programing would be very repetitive, boring and tedious as you’d have to retype all this code over and over again when creating a new app.

Let’s jump to the third line! ‘var str = "Hello, playground”’ is a variable. Think of it as a box that serves as a storage for some content like letters and numbers: ‘str’ is the box and “Hello, playground” is its content. You can create a variable by typing ‘var’ followed by the name or identifier of the variable. Assigning a value to a variable is done by typing the vari-able identifier or the variable name followed by an equals sign and its value.

var myNumber = 5.2var myString = "Hello World!"

Here we create two variables ‘myNumber’ which is equal to 5.2 and ‘myString’ which con-tains the value “Hello World!”

swift code

var str = “hello”println(str)

10SAMPLE CHAPTERS — Learning Swift by Maxime Defauw

compiler computer code

01101100 01101111 01101100

Page 12: Maxime Defauw Learning Swift€¦ · In this course you’ll learn the brand-new Swift language. Apple introduced it at the WWDC 2014, which is an abbreviation for the Apple Worldwide

You don’t have to assign a value to a variables directly when you create it. It is possible to create a variable and assign it’s value later on. But if you do so, you will have to set the type of the variable explicitly. For example, if you want to create a variable that holds an In-teger value (a whole number like 1, 6 or -254), you’ll have to specify the variable to hold an integer. You do this by typing a colon ( : ) after the identifier followed by the type of the vari-able.

var myInteger: IntmyInteger = 5

However if you assign its value directly, Swift is powerful enough to recognize the type of variable and you don’t have to specify the type. Observe the difference between the two.

var explicitInteger: Int = 5var implicitInteger = 4

There are different types of variables in Swift. Here is a list of the basic data types.

Data type Explanation ExampleInt Is used for whole numbers -4, 6, 185, 0

Float Is used for numbers with decimal points 9.6, -7.542, 8534.0Double Is used for bigger numbers with decimal points 4532.000002, -652,628910String Ordered sequences of characters Hello, how are doing?Bool Means true or false True, False

When examining this table above, you may have some questions such as ‘what is the differ-ence between a float and a double?’ or ‘What is a bool used for?’

As the name implies, a double has two times the precision of a float. This means that the result of a calculation stored in a float can be 9,00... , though if it’s stored in a double it would be 8,99...

A bool or boolean is a variable type that can either be true or false. It is used to represent the truth values in an expression. For instance if you want to check in code if 5 is greater than 4, the bool would be true. You’ll learn more about bools in CHAPTER NAME.

11SAMPLE CHAPTERS — Learning Swift by Maxime Defauw

Page 13: Maxime Defauw Learning Swift€¦ · In this course you’ll learn the brand-new Swift language. Apple introduced it at the WWDC 2014, which is an abbreviation for the Apple Worldwide

Constants

A variable is, as the name suggest, variable. This means that if you assign a value to a vari-able, it can be changed later on in the program. A constant on the contrary is, again as the name suggest, constant. This means that once you assign a value to it, it can never be changed. In Swift you create constants just like you create variables, but instead of writing ‘var’ before the identifier, you write ‘let’.

let myName = "Maxime"var myAge = 15

// Next year, my age becomes greater, but my name stays the same. That’s why ‘myName’ is constant and ‘myAge’ is a variable

myAge = 16

// Now ‘myAge’ is equal to 16

Why would you want to use a constant? The answer is simple: for safer and cleaner code and performance optimization. If you ever want to create ‘a box’ for a value, the rule is sim-ple: if it can be a constant, make it a constant. Otherwise make it a variable.

12SAMPLE CHAPTERS — Learning Swift by Maxime Defauw

Page 14: Maxime Defauw Learning Swift€¦ · In this course you’ll learn the brand-new Swift language. Apple introduced it at the WWDC 2014, which is an abbreviation for the Apple Worldwide

Challenge

The best way of learning a new programming language is not by only reading a book or watching videos online. You have to exercise what you’ve learned! This is why there will be some challenges at the end of each chapter. Some are easy, others may be more difficult. But it is very important you try to succeed every challenge at its best! These are the chal-lenges for this chapter:

Create a new playground file named Challenge Chapter 3

Make two variables; one implicit Double, and one explicit Int.

Create a constant with your name

Add a comment beneath explaining the difference between a float and a double.

The solutions for this chapter can be found here https://www.dropbox.com/s/g18gjwgeyvln05t/Challenge%20Chapter%203.playground.zip?dl=0

If you completed these challenges, you can officially code!

13SAMPLE CHAPTERS — Learning Swift by Maxime Defauw