Monday, December 15, 2014

Swift Programming Language: A Quick Start

Swift is Apple’s brand new programming language.

This Swift tutorial will give you a basic tour of the Swift language, including variables, constants, basic types, control flow and more.

Introduction to Playgrounds

A playground is a new type of file that allows you to test out Swift code, and see the results of each line in the sidebar

Playgrounds are a great way to learn about Swift (like you’re doing in this Swift tutorial), to experiment with new APIs, to prototype code or algorithms, or to visualize your drawing code. In the rest of this Swift tutorial, you will be working in this playground.

Playing with the Playground:

Start up Xcode 6, and File -> New -> Playground.
Name the file SwiftTutorial.playground, click Next, and save the file somewhere convenient. Delete everything from your file so you start with a clean slate.

For example, add the following lines to your playground:
let valueOne = 56
let valueTwo = 23
let totalValue = valueOne + valueTwo

As you type these lines, you will see the result of each line on the sidebar.

Variables vs. Constants in Swift

Try adding the following line to the bottom of your playground:
totalValue += 1
You’ll notice an error when you add this line. This is because totalValue is a constant, meaning its value can never change. You declare constants with the keyword let.
You want valueOne to be a variable instead — a value that can change — so you need to declare it with a different keyword: var.
Replace the line that initializes totalValue with the following:
var totalValue = valueOne + valueTwo
Now it works! You may think to yourself, “why not just declare everything with var, since it’s less restrictive?”
Well, declaring things with let whenever possible is best practice, because that will allow the compiler to perform optimizations that it wouldn’t be able to do otherwise. So remember: prefer let!

Explicit vs. Inferred Typing

So far, you haven’t explicitly set any types for these constants and variables, because the compiler had enough information to infer it automatically.
For example, because you set valueOne to 56, the compiler knows 56 is an Int, so it set the type of valueOne to an Int for you automatically.
However, you can set the type explicitly if you want. Try this out by replacing the line that sets valueOne to the following:

let valueOne: Int = 56
You may wonder if you should set types explicitly, or let the compiler infer the types for you. We believe it’s better practice to let the compiler infer types automatically where possible, because then you get one of the main advantages of Swift: concise and easy to read code.
Because of this, switch the line back to inferred typing:
let valueOne = 56

Basic Types and Control Flow in Swift

So far, you’ve seen an example of Int, which is the Swift type that is used for integer values, but there’s a bunch more.

Floats and Doubles

let priceInferred = 19.99
let priceExplicit: Double = 19.99
There are two types for decimal point values like this: Float and Double. Double has more precision, and is the default for inferring decimal values. That means priceInferred is a Double too.


let onSaleInferred = true
let onSaleExplicit: Bool = false
Note that in Swift you use true/false for boolean values (unlike the convention of using YES/NO in Objective-C).


let nameInferred = "Whoopie Cushion"
let nameExplicit: String = "Whoopie Cushion"
Strings are as you’d expect, except note that you no longer use the ‘@’ sign like you do in Objective-C. That might take your muscle memory a bit to get used to! 

If statements and string interpolation

if onSaleInferred {
  println("\(nameInferred) on sale for \(priceInferred)!")
} else {
  println("\(nameInferred) at regular price: \(priceInferred)!")
This is an example of an if statement, just like you’d expect in different languages. The parentheses around the condition are optional, and braces are required even for 1-line statements.
This also shows an example of a new technique called string interpolation. Whenever you want to substitute something in a string in Swift, simply use this syntax: 
\(your expression).
At this point, you may wonder where the println output of this code has gone. To see the println output, bring up the Assistant Editor with View\Assistant Editor\Show Assistant Editor.

No comments:

Post a Comment