NUS iOS Swift Talk

88
Introduction to Swift Swift Syntax and Basics

description

Talk on iOS Swift Basics and Syntax at NUS Institute of Systems Science, an iOS Dev Scout event

Transcript of NUS iOS Swift Talk

Page 1: NUS iOS Swift Talk

Introduction to SwiftSwift Syntax and Basics

Page 2: NUS iOS Swift Talk

Why Are You Here?

Page 3: NUS iOS Swift Talk

Why Are You Here?!

• New to Programming - but want to learn to write iPhone apps

Page 4: NUS iOS Swift Talk

Why Are You Here?!

• New to Programming

• Existing Programmer, no experience with iOS

Page 5: NUS iOS Swift Talk

Why Are You Here?!

• New to Programming

• Existing Programmer, no experience with iOS

• Experienced Obj-C iOS Developer, no experience with Swift

Page 6: NUS iOS Swift Talk

Why Are You Here?!

• New to Programming

• Existing Programmer, no experience with iOS

• Experienced Obj-C iOS Developer, no experience with Swift

• Already knows Swift

Page 7: NUS iOS Swift Talk

Why Are You Here?!

• New to Programming

• Existing Programmer, no experience with iOS

• Experienced Obj-C iOS Developer, no experience with Swift

• Already knows Swift - what are you doing here?

Page 8: NUS iOS Swift Talk

What is Swift, Anyway?

Page 9: NUS iOS Swift Talk

What is Swift, Anyway?!

Apple’s brand new programming language, that aims to make writing iOS apps easier, more flexible and fun.

Page 10: NUS iOS Swift Talk

About Me!

Co-Founder of Saleswhale, a B2B SaaS start-up

Mobile Intelligent Sales Productivityhttp://saleswhale.io

Page 11: NUS iOS Swift Talk

!

• Personally started iOS development 3 years ago

• Started Saleswhale, a mobile-first sales force management software, with beta customers as far as Silicon Valley

• Currently a team of 7 - all technical

• Bootstrapped from iOS consulting

Page 12: NUS iOS Swift Talk

Frequently Thought Questions

!

I’m a beginner. I want to learn to program iOS apps.

Should I learn Objective-C, Swift, or both?

Page 13: NUS iOS Swift Talk

Frequently Thought Questions

!

What is the big fuss about it?

Page 14: NUS iOS Swift Talk

Frequently Thought Questions

!

What is a playground?

Page 15: NUS iOS Swift Talk

Frequently Thought Questions

!

Will Swift give me superpowers?

Page 16: NUS iOS Swift Talk

What You Need• Xcode 6

Page 17: NUS iOS Swift Talk

A long, long time ago..#include <stdio.h>

int main() { printf(“hello, world!”); return 0;}

Page 18: NUS iOS Swift Talk

Today!

println(“Hello, World!”)

Page 19: NUS iOS Swift Talk

What You Will Learn• Basics of the Swift Language

• Playground

• Beyond the Basics

Page 20: NUS iOS Swift Talk

Variables!

var

Page 21: NUS iOS Swift Talk

Variables!

var whale

Page 22: NUS iOS Swift Talk

Variables!

var whale: String

Page 23: NUS iOS Swift Talk

Variables!

var whale: String = “Moby Dick”

Page 24: NUS iOS Swift Talk

Constants and Variables!

let whale: String = “Moby Dick”var weight: Double = 900.20let isBlue: Bool = true

Page 25: NUS iOS Swift Talk

Type Inference!

let whale = “Moby Dick”var weight = 900.20let isBlue = true

Page 26: NUS iOS Swift Talk

For Fun!

let whale = “Moby Dick”var weight = 900.20let isBlue = true let 🐮 = “bull”let 💩 = “poop”

Page 27: NUS iOS Swift Talk

For Fun!

let whale = “Moby Dick”var weight = 900.20let isBlue = true let 🐮 = “bull”let 💩 = “poop”let android = 🐮 + 💩

Page 28: NUS iOS Swift Talk

For Fun!

let whale = “Moby Dick”var weight = 900.20let isBlue = true let 🐮 = “bull”let 💩 = “poop”let android = 🐮 + 💩

Answer?

Page 29: NUS iOS Swift Talk

More String Power (String Interpolation)

Page 30: NUS iOS Swift Talk

More String Power (String Interpolation)

!

let price = 2, copies = 100000

Page 31: NUS iOS Swift Talk

More String Power (String Interpolation)

!

let price = 2, copies = 100000

let profit = “\(price) times \(copies) is \(price * copies)”

Page 32: NUS iOS Swift Talk

More String Power (String Interpolation)

!

let price = 2, copies = 100000

let profit = “\(price) times \(copies) is \(price * copies)”

Evaluate to?

Page 33: NUS iOS Swift Talk

Collection Types

Page 34: NUS iOS Swift Talk

Collection Types!

Arrays and Dictionaries

Page 35: NUS iOS Swift Talk

Collection Types!

What are Arrays?

Page 36: NUS iOS Swift Talk

Collection Types!

What are Arrays?!

A group of data!

Page 37: NUS iOS Swift Talk

Collection Types!

What are Arrays?!

A group of data!

100 DVD holder that sits on your floor and holds 100 dvds

Page 38: NUS iOS Swift Talk

Collection Types!

What are Dictionaries?!

Page 39: NUS iOS Swift Talk

Collection Types!

What are Dictionaries?!

Key-value pairs!

Page 40: NUS iOS Swift Talk

Collection Types!

What are Dictionaries?!

Key-value pairs!

{! “Godzilla” ! ! : “Romance”,! “Iron Man 2” !! : “Satire”,! “The Social Network”! : “Comedy”}

Page 41: NUS iOS Swift Talk

Array and Dictionary Literals

Page 42: NUS iOS Swift Talk

Array and Dictionary Literals!

Easiest way to create Arrays and Dictionaries

Page 43: NUS iOS Swift Talk

Array and Dictionary Literalsvar DVDs = [“Shawshank Redemption”, “The Godfather”, “Casablanca”]

Page 44: NUS iOS Swift Talk

Array and Dictionary Literalsvar DVDs = [“Shawshank Redemption”, “The Godfather”, “Casablanca”]

var genres = [“Godzilla” : “Romance, “Iron Man 2” : “Satire”, “The Social Network” : “Comedy”]

Page 45: NUS iOS Swift Talk

Array and Dictionary Literalsvar DVDs = [“Shawshank Redemption”, “The Godfather”, “Casablanca”]

var genres = [“Godzilla” : “Romance, “Iron Man 2” : “Satire”, “The Social Network” : “Comedy”]

Can work with other types

Page 46: NUS iOS Swift Talk

Loops

Page 47: NUS iOS Swift Talk

Loopswhile hazPetrol { drive()}

for var miles = 1; miles <= 10; ++miles { travel(miles)}

Page 48: NUS iOS Swift Talk

For-In

Page 49: NUS iOS Swift Talk

For-In: Rangesfor number in 1..5 { println(“\(number) times 2 is \(number * 2)”) }

Page 50: NUS iOS Swift Talk

For-In: Arraysfor movie in [“Godzilla”, “Iron Man”, “Jack”] { println(“\(movie) is a nice film.”) }

Page 51: NUS iOS Swift Talk

For-In: Dictionarylet dictionary = [“a” : 1, “b” : 2, “c” : 3]

for (key, value) in dictionary { println(“\(key) maps to \(value)”)}

Page 52: NUS iOS Swift Talk

Modifying an Arrayvar mugunthCars = [“BMW”, “Audi”, “VW”]

Page 53: NUS iOS Swift Talk

Modifying an Arrayvar mugunthCars = [“BMW”, “Audi”, “VW”]println(mugunthCars[2])

Page 54: NUS iOS Swift Talk

Modifying an Arrayvar mugunthCars = [“BMW”, “Audi”, “VW”]println(mugunthCars[2])mugunthCars += “Volvo”

Page 55: NUS iOS Swift Talk

Modifying an Arrayvar mugunthCars = [“BMW”, “Audi”, “VW”]println(mugunthCars[2])mugunthCars += “Volvo”mugunthCars += [“Toyota”, “Hyundai”, “Honda”]

Page 56: NUS iOS Swift Talk

Modifying an Arrayvar mugunthCars = [“BMW”, “Audi”, “VW”]println(mugunthCars[2])mugunthCars += “Volvo”mugunthCars += [“Toyota”, “Hyundai”, “Honda”]mugunthCars[0] = [“Proton”]

Page 57: NUS iOS Swift Talk

Modifying an Arrayvar mugunthCars = [“BMW”, “Audi”, “VW”]println(mugunthCars[2])mugunthCars += “Volvo”mugunthCars += [“Toyota”, “Hyundai”, “Honda”]mugunthCars[0] = [“Proton”]mugunthCars[3..5] = [“Lamborghini”, “Ferrari”]

Page 58: NUS iOS Swift Talk

Modifying a Dictionaryvar ratings = [“Apple” : “Good”, “Blackberry” : “Non Existent”]

Page 59: NUS iOS Swift Talk

Modifying a Dictionaryvar ratings = [“Apple” : “Good”, “Blackberry” : “Non Existent”]ratings[“Android”] = “Good”

Page 60: NUS iOS Swift Talk

Modifying a Dictionaryvar ratings = [“Apple” : “Good”, “Blackberry” : “Non Existent”]ratings[“Android”] = “Good”ratings[“Android”] = “Bad”

Page 61: NUS iOS Swift Talk

Short Break

Page 62: NUS iOS Swift Talk

Retrieving a Value from a Dictionary

let ratings = [“Apple” : “Good”, “Blackberry” : “Non Existent”]

let possibleRating = ratings[“Windows”]

Page 63: NUS iOS Swift Talk

Optionalslet ratings = [“Apple” : “Good”, “Blackberry” : “Non Existent”]

let possibleRating: String? = ratings[“Windows”]

Page 64: NUS iOS Swift Talk

Optionalslet ratings = [“Apple” : “Good”, “Blackberry” : “Non Existent”]

let possibleRating: String? = ratings[“Windows”]

if possibleRating == nil { println(“Rating for Windows not found”) }

Page 65: NUS iOS Swift Talk

Optionalslet ratings = [“Apple” : “Good”, “Blackberry” : “Non Existent”]

let possibleRating: String? = ratings[“Windows”]

if possibleRating == nil { println(“Rating for Windows not found”)} else { let windowsRating = possibleRating! println(“Rating for Windows is \(windowsRating)”}

Page 66: NUS iOS Swift Talk

Functionsfunc sayMeow() { println(“Meow!”)}

Page 67: NUS iOS Swift Talk

Functions and Parametersfunc sayMeow(name: String) { println(“Meow \(name)!”)}

Page 68: NUS iOS Swift Talk

Functions and Parametersfunc sayMeow(name: String) { println(“Meow \(name)!”)}

sayMeow(“Tom”)

!

!

!

Page 69: NUS iOS Swift Talk

Functions and Parametersfunc sayMeow(name: String) { println(“Meow \(name)!”)}

sayMeow(“Tom”)

!

!

!

Meow Tom!

Page 70: NUS iOS Swift Talk

Default Parameter Valuesfunc sayMeow(name: String = “Jerry”) { println(“Meow \(name)!”)}

sayMeow(“Tom”)

!

!

!

Meow Tom!

Page 71: NUS iOS Swift Talk

Returning Multiple Valuesfunc getDoubleDegree() -> (String, String) { // …after a few years… return (“Comp Sci”, “Electrical Engineering”)}

!

!

!

!

Page 72: NUS iOS Swift Talk

Tuples(3, 6, 9)

(42, “Meaning of Life”)

!

!

!

!

Page 73: NUS iOS Swift Talk

Tuples(3, 6, 9) // (Int, Int, Int)

(42, “Meaning of Life”) // (Int, String)

!

!

!

!

Page 74: NUS iOS Swift Talk

Using a Tuplefunc getDoubleDegree() -> (String, String) { // …after a few years… return (“Comp Sci”, “Electrical Engineering”)}

let (firstDegree, secondDegree) = getDoubleDegree()

println(“Got \(firstDegree) and \(secondDegree)”)

!

!

!

Page 75: NUS iOS Swift Talk

Classesclass Animal {

// properties // methods // initializers

}

Page 76: NUS iOS Swift Talk

Classesclass Animal {

// properties // methods // initializers

}

!

No header filesNo base class

Page 77: NUS iOS Swift Talk

Class Inheritanceclass Animal {

}

class Cow: Animal {

}

Page 78: NUS iOS Swift Talk

Propertiesclass Animal { var numberOfLegs = 0}

Page 79: NUS iOS Swift Talk

Propertiesclass Animal { var numberOfLegs = 0}

// Read-write property (var)

Page 80: NUS iOS Swift Talk

Propertiesclass Animal { var numberOfLegs = 0}

// Read-write property (var)// Constant (let)

Page 81: NUS iOS Swift Talk

Initialisingclass Animal { var numberOfLegs = 0}

let someAnimal = Animal()

Page 82: NUS iOS Swift Talk

Initialisingclass Animal { var numberOfLegs = 0}

let someAnimal = Animal()

// New instance of Animal

Page 83: NUS iOS Swift Talk

Dot Syntaxlet someAnimal = Animal()

println(someAnimal.numberOfLegs) // 0 legs

Page 84: NUS iOS Swift Talk

Dot Syntaxlet someAnimal = Animal()

println(someAnimal.numberOfLegs) // 0 legs

someAnimal.numberOfLegs = 5

println(someAnimal.numberOfLegs) // 5 legs

Page 85: NUS iOS Swift Talk

Class Initializationclass Animal { var numberOfLegs: Double init(numberOfLegs: Double) { self.numberOfLegs = numberOfLegs } } let someAnimal = Animal(numberOfLegs: 4)

Page 86: NUS iOS Swift Talk

Class Initializationclass Animal { var numberOfLegs: Double init(numberOfLegs: Double) { self.numberOfLegs = numberOfLegs } } let someAnimal = Animal(numberOfLegs: 4)

Page 87: NUS iOS Swift Talk

PracticeWrite a CPF Calculator program based on what you learnt that !

• Can calculate your CPF Contribution based on salary!

• Can dynamically adjust the contributed based on age!

• Calculates the total interest paid out in your OA, SA given a variable number of years

Page 88: NUS iOS Swift Talk

Thank You!

Gabriel [email protected]

Mobile Intelligent Sales Productivityhttp://saleswhale.io