Swift 2 intro

20

Transcript of Swift 2 intro

Page 1: Swift 2 intro
Page 2: Swift 2 intro

SWIFT 2 INTRODUCTION

Page 3: Swift 2 intro

SWIFT

Powerful new language created by Apple

Works side by side with Objective-C

Syntax is closer to popular languages

Page 4: Swift 2 intro

VAR VS LET

var for regular variables

let for constants

camelCase your variable names

var variableNumber = 5let constantNumber = 3.14

Page 5: Swift 2 intro

TYPE INFERENCE

Swift is strongly typed

The type will be inferred when not specified

var explicitVariable: Int = 10var inferredVariable = 10var explicitVariable2: Double = 10

Page 6: Swift 2 intro

OPTIONALS

Regular types must always have a value

Optional types used when value may be nil

Denote optionals with a ?

var alwaysAnInt: Int = 15var maybeAnInt: Int? = 15maybeAnInt = nil

Page 7: Swift 2 intro

UNWRAPPING AN OPTIONAL

Optionals must be unwrapped before using

Can be checked for nil with an if-statement

Once checked, force unwrap optionals with !

if maybeAnInt != nil { print("maybeAnInt contains \(maybeAnInt!)")}

Page 8: Swift 2 intro

OPTIONAL BINDING

Cleaner way to unwrap optionals

"Binds" value of optional to new variable

if let definitelyAnInt = maybeAnInt { print("maybeAnInt contains \(definitelyAnInt)")}

Page 9: Swift 2 intro

IMPLICITLY UNWRAPPED OPTIONALS

Trigger a runtime error when accessed if

the value is nil

Denoted using a !

var alwaysAString: String! = nillet stringLength = alwaysAString.characters.count

Page 10: Swift 2 intro

OPTIONAL CHAINING

Cannot access optional without unwrapping

Chaining only accesses if value not nil

var optionalArray: [Int]? = [ 1, 2, 3, 4 ]var arrayLength = optionalArray?.count

Page 11: Swift 2 intro

ARRAYS

Can only contain a single type

Immutable arrays use let, mutable use var

count, isEmpty, append, insertlet groceryList: [String] = ["eggs", "milk"]var mutableGroceryList = ["eggs", "milk"]var item = mutableGroceryList[0]

Page 12: Swift 2 intro

DICTIONARIES

Hold key-value pairs

Keys and values each can only be one type

Best to use optional binding when accessing

var cities = ["New York City" : "USA", "London" : "UK"]cities["London"]cities["London"] = nil

Page 13: Swift 2 intro

CLASSES VS STRUCTSclass PersonRefType { let name:String var age:Int // ..}// 1let peter = PersonRefType(name: "Peter", age: 36)// 2let peter2 = peter// 3peter2.age = 25// peter {"Peter", 25}// peter2 {"Peter", 25}

Page 14: Swift 2 intro

CLASSES VS STRUCTS

struct Person { let name:String var age:Int}// 1let petra = Person(name:"Petra", age:25)// 2var petra2 = petra// 3petra2.age = 20// petra {"Petra", 25}// petra2 {"Petra", 20}

Page 15: Swift 2 intro

NEW IN SWIFT 2

Page 16: Swift 2 intro

ERROR HANDLING

Functions and methods can throw

Calling functions need to handle errors

Page 17: Swift 2 intro

ERROR HANDLINGenum TripError: ErrorType { case NoWaypointsProvided}

func createTrip() throws { throw TripError.NoWaypointsProvided}

func buttonTapped() { do { try createTrip() } catch TripError.NoWaypointsProvided { print("oh-oh") } catch { print("other error") }}

Page 18: Swift 2 intro

GUARD

Alternative to optional binding

func printRating() { guard let customerRating = rating?.customerRating, merchantRating = rating?.merchantRating, ratingDate = rating?.ratingDate else { return } print("Customer: \(customerRating), Merchant: \(merchantRating), Rating Date: \(ratingDate)")}

Page 19: Swift 2 intro

PROTOCOL EXTENSIONS

Provide implementation as part of protocolprotocol Vehicle { var speed: Double { get set } func travelDuration(distance: Double) -> Double }

extension Vehicle { func travelDuration(distance: Double) -> Double { return distance / speed } }

Page 20: Swift 2 intro