57
IOS BOOTCAMP 7° EDITION #pragma mark @ WCAP Catania 06/06/2015, v 1.2-S

Introduction to Swift

Embed Size (px)

Citation preview

Page 1: Introduction to Swift

IOS BOOTCAMP7° EDITION

#pragma mark

@ WCAP Catania

06/06/2015, v 1.2-S

Page 2: Introduction to Swift

THANKS TO

Youthub Catania

Motorsquare

Page 3: Introduction to Swift

#PRAGMA MARK

Started by a group of friends in 2012

Aim: create a community of iOS developers

Non-profit org since Sept. 2013

Page 4: Introduction to Swift

#PRAGMA MARK

In 3 years:

Organized a dozen events in northern Italyall over Italy! !

Created an active on-line community:fb.com/groups/pragmamark

2 editions of the #Pragma Conference

Page 5: Introduction to Swift

#PRAGMA MARK

Next edition: Florence, 9-10 Oct 2015

www.pragmaconference.com

Page 6: Introduction to Swift

JUST FOR TODAY'S ATTENDEES15% DISCOUNT CODE:! PragmaLovesCatania "

(limited to 30 tickets, so be quick!)

Page 7: Introduction to Swift

LET'S GET THE BOOTCAMP STARTED!

Page 8: Introduction to Swift

HELLO WORLD▸ Matteo Battaglio▸ iOS dev @ Codermine

▸ #pragma mark co-founder

Page 9: Introduction to Swift

SLIDES

http://bit.ly/swift-introduction

Page 10: Introduction to Swift

HISTORY

Page 11: Introduction to Swift

LANGUAGE FATHER

Chris Lattner

Page 12: Introduction to Swift

LANGUAGE BIRTH

I started work on the Swift Programming Language in July of 2010. I implemented much of the basic language

structure, with only a few people knowing of its existence. A few other (amazing) people started

contributing in earnest late in 2011, and it became a major focus for the Apple Developer Tools group in July

2013.— Chris Lattner

Page 13: Introduction to Swift

LANGUAGE INSPIRATION

The Swift Programming Language greatly benefited from the experiences hard-won by many other languages in the field, drawing ideas from Objective-C, Rust, Haskell, Ruby,

Python, C#, CLU, and far too many others to list.— Chris Lattner

Page 14: Introduction to Swift

PRINCIPLES

Page 15: Introduction to Swift

SWIFT PROGRAMMING LANGUAGEimperative, functional, object oriented, multi-paradigm,

static, strong typed, type safe, inferred,general purpose, compiled, fast,

modern, elegant, clean,funny, happy,❤

Page 16: Introduction to Swift

SYNTAX

Page 17: Introduction to Swift

Immutable & Mutable

Functions & Closures

Tuples & Pattern Matching

Data Types & Instances

Protocols & Extensions

Generics & Optionals

Page 18: Introduction to Swift

IMMUTABLE & MUTABLE

Page 19: Introduction to Swift

CONSTANTS & VARIABLESTYPE ANNOTATIONS & TYPE INFERENCE

let constant: Int = 1 // Type Annotationslet constant = 1 // Type Inference (as Int)

let constant = 1 // readonly, cannot be re-assignedconstant = 2 // ❌ ERROR !!!

var variable = 1 // readwrite, can be re-assignedvariable = 2 // OK

By convention you should prefer to use 'let' over 'var'

Page 20: Introduction to Swift

CONSTANTS & VARIABLESUNICODE CHARACTERS IN CONSTANT & VARIABLE NAMES

let !!!! = 4var """"" = 5

! KILLER APPLICATION "

Page 21: Introduction to Swift

VALUE & REFERENCE TYPESENUMERATIONS & STRUCTURES VS CLASSES

▸ Enumerations & Structures are passed by Value▸ Classes are passed by Reference

ENUMERATIONS & STRUCTURES ARE ALWAYS COPIED WHEN THEY ARE PASSED AROUND IN THE CODE, AND DO NOT USE REFERENCE COUNTING.

Page 22: Introduction to Swift

STRUCTURESMANY SWIFT LIBRARY'S BASE TYPES ARE STRUCTURES

// Stringlet string = "Swift"

// Characterlet character: Character = "c"

// Arraylet array = ["A", "B"]

// Dictionarylet dictionary = ["First" : 1, "Second" : 2]

Page 23: Introduction to Swift

STRUCTURESCONSTANT & VARIABLE ARRAYS

let constantArray = ["A"]var variableArray = ["A"]

constantArray = ["X"] // ❌ ERROR !!!constantArray[0] = "Y" // ❌ ERROR !!!constantArray.append("B"); // ❌ ERROR !!!constantArray.removeAtIndex(0) // ❌ ERROR !!!

variableArray = ["X"] // ["X"]variableArray[0] = "A" // ["A"]variableArray.append("B"); // ["A", "B"]variableArray.removeAtIndex(0) // ["B"]

Page 24: Introduction to Swift

FUNCTIONS & CLOSURES

Page 25: Introduction to Swift

FUNCTIONSFIRST-CLASS FUNCTION

▸ assign a function to variable▸ pass function as argument to another function

▸ return a function from a function▸ functional programming patterns: map, filter, ...

Page 26: Introduction to Swift

FUNCTIONSDECLARATION & CALL

// declarationfunc foo(parameter1: Type1, parameter1: Type2) -> ReturnType { /* function body */}// callfoo(argument1, argument2)

// external and local parameter namesfunc bar(externalParameterName localParameterName: Type) { /* body use localParameterName */}// call must use externalParameterName labelbar(externalParameterName: argument)

Page 27: Introduction to Swift

CLOSURESMEANING & SYNTAX

// Closures are blocks of functionality that can be passed around

{ (parameter1: Type1, parameter2: Type2) -> ReturnType in / * ... */}

// Closures can capture and store references to any constants and// variables from the context in which they are defined.

Page 28: Introduction to Swift

FUNCTIONS VS CLOSURES

▸ Global functions:NAMED CLOSURES / DO NOT CAPTURE ANY VALUES

▸ Nested functions:NAMED CLOSURES / CAPTURE VALUES FROM ENCLOSING FUNCTION

▸ Closure expressions:UNNAMED CLOSURES / CAPTURE VALUES FROM THEIR CONTEXT

Page 29: Introduction to Swift

FUNCTIONS VS CLOSURESSORTING WITHOUT CLOSURES

// define a function which takes an array and a sorting algorithmfunc sorted(array: [Int], algorithm: (Int, Int) -> Bool) -> [Int] { /* apply the sorting algorithm to the array */}

// define an arraylet array = [1, 2, 3]

// define a sorting algorithm funtionfunc backwards(i1: Int, i2: Int) { return i1 > i2}

// call sortedvar reversed = sorted(array, backwards)

Page 30: Introduction to Swift

FUNCTIONS VS CLOSURESSORTING WITH CLOSURES 1/2

// Fully declared closurereversed = sorted(array, { (i1: Int, i2: Int) -> Bool in return i1 > i2 } )

// Infer closure typereversed = sorted(array, { (i1, i2) in return i1 > i2 } )

// Implicit returnsreversed = sorted(array, { (i1, i2) in i1 > i2 } )

Page 31: Introduction to Swift

FUNCTIONS VS CLOSURESSORTING WITH CLOSURES 2/2

// Shorthand argument namesreversed = sorted(array, { $0 > $1 } )

// Trailing closure: outside of () only if it's function’s final argumentreversed = sorted(array) { $0 > $1 }

// Operator functionsreversed = sorted(array, >)

Page 32: Introduction to Swift

TUPLES & PATTERN MATCHING

Page 33: Introduction to Swift

TUPLESLIGHTWEIGHT CONTAINERS FOR MULTIPLE VALUES

let complex = (1.0, -2.0) // Compound Type: (Double, Double)let (real, imag) = complex // Decomposelet (real, _) = complex // Underscores ignore value

// Access by indexlet real = complex.0let imag = complex.1

// Name elementslet complex = (real: 1.0, imag: -2.0)let real = complex.real

Page 34: Introduction to Swift

PATTERN MATCHINGlet point = (42, 42)switch point { // break by default

case (0, 0): println("match a specific tuple")

case (_, 0): println("ignore undescore value")

case (let x, 1): println("bind a value to x")

case let (x, y) where x == y: println("bind values which satify the where clause")

default: println("must be exhaustive")}

Page 35: Introduction to Swift

DATA TYPES & INSTANCES

Page 36: Introduction to Swift

ENUMERATIONS, STRUCTURES & CLASSESCOMMON CAPABILITIES

▸ Stored & Computed Properties▸ Methods, Subscripts & Initializers▸ Protocols & Extensions▸ Access Control

Page 37: Introduction to Swift

ENUMERATIONS, STRUCTURES & CLASSESpublic class Vehicle {

// stored instance property private let name: String

// computed instance property public var formattedName: String { return "vehicle: \(name)" }

// type property class var maxSpeed : Int { return 299_792_458 }

// initializer init (name: String) { self.name = name }

// instance method internal func move() { /* ... */ }}

Page 38: Introduction to Swift

CLASSES OnlyADDITIONAL CAPABILITIES:

▸ Inheritance▸ Type casting▸ Deinitializers

▸ Reference counting

Page 39: Introduction to Swift

CLASSES Onlypublic class Car: Vehicle { // inheritance

// override public override var formattedName: String { return "car: \(name)" }

// designated initializer override init(name: String) { super.init(name: name);}

// convenience initializer convenience init() { self.init(name: "Car") }

// deinitializer deinit { /* clean up */ }}

let vehicle = Car(name: "Supercar")let car = vehicle as Car // Type Casting

Page 40: Introduction to Swift

PROTOCOLS & EXTENSIONS

Page 41: Introduction to Swift

PROTOCOLS// Available for Enumerations, Structures & Classes

protocol SomeProtocol { // define a set of requirements

var instanceProperty: Type { get set } // require instance properties

class var typeProperty: Type { get set } // require type properties

func someMethod() // require instance methods

class func someTypeMethod() // require type methods

init() // require initializers}

Page 42: Introduction to Swift

EXTENSIONS// Available for Enumerations, Structures & Classes

extension SomeType: SomeProtocol { // add protocol conformance

var stored = 1 // ❌ ERROR !!! // CANNOT add store properties !

var computed: String { /* ... */ } // add computed properties

func method() { /* ... */ } // add methods

subscript(i: Int) -> String { /* ... */ } // add subscripts

init(parameter: Type) { /* ... */ } // add initializers

enum SomeEnum { /* ... */ } // add nested types}

Page 43: Introduction to Swift

EXTENSIONSEXTENSIONS CAN EXTEND ALSO SWIFT LIBRARY TYPES

extension Int { func times(task: () -> ()) { for i in 0 ..< self { task() } }}

3.times({ println("Developer! ") })

// Developer! Developer! Developer! !

Page 44: Introduction to Swift

GENERICS & OPTIONALS

Page 45: Introduction to Swift

GENERICS// Specific Functionsfunc swapTwoStrings(inout a: String, inout b: String) { let temporaryA = a; a = b; b = temporaryA }

func swapTwoDoubles(inout a: Double, inout b: Double) { let temporaryA = a; a = b; b = temporaryA }

// Generic Functionfunc swapTwoValues<T>(inout a: T, inout b: T) { let temporaryA = a; a = b; b = temporaryA }

var someInt = 1, anotherInt = 2swapTwoValues(&someInt, &anotherInt) // T == Int// someInt == 2, anotherInt == 1

Page 46: Introduction to Swift

GENERICSWHERE CLAUSES ON TYPE CONSTRAINTS

protocol Container { typealias ItemType /* ... */ }

func allItemsMatch< C1: Container, C2: Container where C1.ItemType == C2.ItemType, C1.ItemType: Equatable> (container1: C1, container2: C2) -> Bool {

if container1.count != container2.count { return false }

for i in 0..<container1.count { if container1[i] != container2[i] { return false } } return true // all items match }}

Page 47: Introduction to Swift

OPTIONALSAN OPTIONAL VALUE EITHER CONTAINS A VALUE OR NIL

var optionalInt: Int? = 42optionalInt = nil // to indicate that the value is missing

var optionalDouble: Double? // automatically sets to nil

// Check if niloptionalDouble == niloptionalDouble != nil

Page 48: Introduction to Swift

OPTIONALS// Force unwraplet optionalInt: Int? = 42let definitelyInt = optionalInt! // throws runtime error if nil

// Optional bindingif let definitelyInt = optionalInt { // runs if optionalInt is not nil and sets definitelyInt: Int}

// Implicitly Unwrapped Optionalsvar assumedInt: Int! // set to nilassumedInt = 42

var implicitInt: Int = assumedInt // do not need an exclamation markassumedInt = nilimplicitInt = assumedInt // ❌ RUNTIME ERROR !!!

Page 49: Introduction to Swift

OPTIONALSclass Car { var model: String init(model: String) { self.model = model }

func jump() -> String? { if model == "Supercar" { return "!⚡" } else { return nil } }}

// Return type of chaining is always optionalvar car1: Car? = nil; car1?.model // nil: String?var car2: Car? = Car(model: "Supercar"); car2?.model // "Supercar": String?

// Chain on optional return valuecar1?.jump()?.hasPrefix("!") // type Bool?

Page 50: Introduction to Swift

OPTIONALSA GENERIC ENUMERATION WITH ASSOCIATED VALUE

enum Optional<T> : Reflectable, NilLiteralConvertible { case None case Some(T)

/* ... */}

var maybeInt1: Int? = nilvar maybeInt2: Optional<Int> = .None

maybeInt1 = 42maybeInt2 = .Some(42)

Page 51: Introduction to Swift

FINAL THOUGHTS

Page 53: Introduction to Swift

OPEN SOURCE SWIFT ?

Guys, feel free to make up your own dragons if you want, but your speculation is just that: speculation. We literally have not even discussed this yet, because we have a ton

of work to do [...] You can imagine that many of us want it to be open source and part of llvm, but the discussion

hasn't happened yet, and won't for some time.— Chris Lattner @ llvmdev

Page 54: Introduction to Swift

WHAT SWIFT IS MISSING?[ SWIFT 1.2 IN XCODE 6.3 ]

▸ Compiler attributes and Preprocessor▸ Exceptions, KVO, KVC and proper Reflection

HTTPS://GITHUB.COM/KSM/SWIFTINFLUX

Page 55: Introduction to Swift

SWIFT IN PRODUCTION?

▸ Companies are doing it▸ Freelances are doing it▸ Many of us are doing it▸ But be careful if you do it!

A few apps that were built using Swift @ apple.com

Page 56: Introduction to Swift

WWDCnext week (8-12 June)

Great expectations for Swift...

Page 57: Introduction to Swift

THANKSYou can find me on the Internet!

twitter ! @m4dbatgithub ! madbat

" [email protected]