Infinum iOS Talks #1 - Swift done right by Ivan Dikic

32
Swift done right IVAN ĐIKIĆ

Transcript of Infinum iOS Talks #1 - Swift done right by Ivan Dikic

Page 1: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

Swift done right

IVAN ĐIKIĆ

Page 2: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

INTRO

Page 3: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

• value types

• protocols

• functions as first class citizens

• generics

4 MAIN PILLARS OF SWIFT

Page 4: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

SIDE NOTE: LET vs. VAR

let theAnswer = 42 // IMMUTABLE var theAnswer = 42 // MUTABLE

• var defines ordinary variable • let defines constant v

Page 5: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

01 VALUE TYPES

Page 6: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

SWIFT TYPES

• value types • instances keeps unique copy of its data • struct, enum or tuple • array, dictionary, set …

• reference types • instances share a single copy of the data • class

• Swift STL - 90% value types

Page 7: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

EXAMPLE: value vs. reference

// Value type example struct S { var data: Int = -1 } var a = S() var b = a a.data = 42

print("\(a.data), \(b.data)") // prints "42, -1"

// Reference type example class C { var data: Int = -1 } var x = C() var y = x x.data = 42

print("\(x.data), \(y.data)") // prints "42, 42"

Page 8: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

WHY VALUE TYPES

• reason about code

• performance

• threadsafe

Page 9: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

HOW TO CHOOSE BETWEEN THE TWO

• value types (==) • you want copies to have independent state • data will be used in code across multiple threads

• examples • data model layer • API routers (e.g. Alamofire Router)

Page 10: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

HOW TO CHOOSE BETWEEN TWO

• reference types (===, ==) • you want copies to have mutable, shared state • Cocoa

• examples • UIKit subclasses • API Managers • Singletons

Page 11: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

02 HIGHER ORDER FUNCTIONS

Page 12: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

“The highest level of abstraction possible, and no lower than the level at which you are

expert.”

Page 13: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

IN SWIFT EVERYTHING IS A FUNCTION

• +, -, …

• you can pass them around

• you can apply them partially

• higher order functions • map, filter, reduce and friends

Page 14: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

EXAMPLE: MAP

let moneyArray = [10, 20, 30, 40] var stringArray: [String] = []

// Naive approach for money in moneyArray { stringArray.append("\(money)$") }

// Swift approach stringArray = moneyArray.map { (money: Int) -> String in return "\(money)$" }

Page 15: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

EXAMPLE: FILTER

let moneyArray = [10, 20, 30, 40] var filteredArray : [Int] = [] // Naive approach for money in moneyArray { if (money > 30) { filteredArray += [money] } }

// Swift approach filteredArray = moneyArray .filter { (money: Int) -> Bool in return money > 30 }

Page 16: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

EXAMPLE: REDUCE

let moneyArray = [10, 20, 30, 40] var sum = 0 // Naive approach for money in moneyArray { sum = sum + money }

// Swift approach sum = moneyArray .reduce(0, combine: { (accumulator: Int, money: Int) -> Int in return accumulator + money })

Page 17: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

IN SWIFT EVERYTHING IS A FUNCTION

• can be reduced to a simple for in loop

• nothing inherently different or special about how they work

• they can elevate your perspective about common

programming tasks

Page 18: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

03 PROTOCOLS

Page 19: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

SWIFT IS A PROTOCOL ORIENTED PROGRAMMING LANGUAGE

• single inheritance

• inheritance only for classes

• STL includes 54 public protocols • can do - “able” - `RawRepresentable` • is a - “type” - `CollectionType` • can be - “convertible” - `CustomStringConvertible`

• prefer composition instead of inheritance

Page 20: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

EXAMPLE: inheritance

UIVIEWCONTROLLER

ROOT VIEW CONTROLLER

PROGRESSABLE VIEW CONTROLLER

LOGIN VIEW CONTROLLER

Page 21: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

EXAMPLE: composition

LOGIN VIEW CONTROLLER PROGRESSABLE XY

Page 22: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

EXAMPLE: protocol

protocol Progressable: NSObjectProtocol { func showLoading(message: String?)

. . . }

final class LoginViewController: UIViewController, Progressable { func showLoading(message: String?) { } . . . }

Page 23: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

PROTOCOL EXTENSIONS

• extremely powerful feature

• useful for providing a default implementations

Page 24: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

EXAMPLE: protocol extensionsprotocol Progressable: NSObjectProtocol { func showLoading(message: String?) . . . } extension Progressable where Self: UIViewController {

func showLoading(msg:String?) { SVProgressHUD.showWithStatus(

msg, maskType: SVProgressHUDMaskType.Gradient)

} . . . } final class LoginViewController: UIViewController, Progressable { ... }

Page 25: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

04 GENERICS

Page 26: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

GENERICS

• PROBLEM: • write code that is type agnostic

• SOLUTION • <T>

Page 27: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

EXAMPLE: generic approach

protocol NumericType { func +(lhs: Self, rhs: Self) -> Self }

extension Float : NumericType {} extension Int : NumericType {}

func sum<T: NumericType>(x x:T, y:T) -> T { return x + y } sum(x: 1, y: 2) sum(x: 1.5, y: 2.1)

Page 28: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

EXAMPLE: Array

public struct Array<Element> : ... { . . . }

var stringsArray = ["A","B","C"]

Page 29: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

05 OPTIONALS

Page 30: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

SWIFT’S WAY OF REPRESENTING NOTHINGNESS

• is a type that can represent • wrapped value • nothing

• is a concept that does exists in other languages (Haskell, Scala)

• is a type that supports usage of higher order functions on it

Page 31: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

EXAMPLE: optional value

var perhapsInt: Int?

• WRONG: • “This is an Int, which is optional”

• RIGHT: • “This is an Optional, which may or may not hold and Int”

Page 32: Infinum iOS Talks #1 - Swift done right by Ivan Dikic

Q&A