swift 2

21
A short introduction to Swift 2

Upload: jens-ravens

Post on 08-Aug-2015

53 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Swift 2

A short introduction to Swift 2

Page 2: Swift 2

syntax goodies

Page 3: Swift 2

Swift 1.2

- Objective-C generics - Pattern Matching - Availability Checking - do/while is now repeat - unit testing via @testable - Markdown support for documentation

(including images)

Page 4: Swift 2

nested generic value types

public enum Result<T> { case Success(T) case Error(NSError) }

public enum Result<T> { case Success(Box<T>) case Error(NSError) }

Swift 1.2

Swift 2

Page 5: Swift 2

guard

func someFunction(value: Int?){ guard let value = value else {return} let calc = 1 + value }

Page 6: Swift 2

defer

func awesomeStuff() { someCalculation() defer { cleanUp() } otherStuff() }

Page 7: Swift 2

available

if #available(iOS 8.0, *) { // new features from iOS 8 } else { // a fallback feature }

Page 8: Swift 2

error handling

Page 9: Swift 2

calling a throwing function

do { try otherStuff() } catch let error { // handle the error }

Page 10: Swift 2

throw an error

func otherStuff() throws { throw NSError(domain: "OutOfIdeasException",

code: 404, userInfo: nil) }

Page 11: Swift 2

throw a custom error

enum Error: ErrorType { case NetworkFailed case DeviceOverheated }

func otherStuff() throws { throw Error.NetworkFailed }

func awesomeStuff() { do { try otherStuff() } catch Error.NetworkFailed { // handle the error } catch { // unspecific error } }

Page 12: Swift 2

force try

enum Error: ErrorType { case NetworkFailed case DeviceOverheated }

func otherStuff() throws { throw Error.NetworkFailed }

func awesomeStuff() { try! otherStuff() }

Page 13: Swift 2

protocol extensions

Page 14: Swift 2

default implementationsprotocol Drinkable { func drink() }

struct Beer: Drinkable { }

struct Mate: Drinkable { func drink() { print("Mate specific drinking.") } }

extension Drinkable { func drink() { print("Time to drink!") } }

Beer().drink()

Page 15: Swift 2

protocol oriented programming

https://developer.apple.com/videos/wwdc/2015/?id=408

Page 16: Swift 2

but there is one more thing

Page 17: Swift 2

Swift becoming open source

Page 18: Swift 2

• Swift source code will be released under an OSI-approved permissive license.

• Contributions from the community will be accepted — and encouraged.

• At launch we intend to contribute ports for OS X, iOS, and Linux. • Source code will include the Swift compiler and standard library.

Page 19: Swift 2

Nest

https://github.com/nestproject

Page 20: Swift 2

https://github.com/nestproject/NestBox

import NestBox

let server = serve("127.0.0.1", 8080) { environ in return ("200 OK", [], "Hello World!") }

server()

Nest

Page 21: Swift 2

Thank you.

@JensRavensgithub.com/jensravens