Upgrade to Pro — share decks privately, control downloads, hide ads and more …

O que há de novo no Swift 2.0

O que há de novo no Swift 2.0

Apresentação no CocoaHeads Campinas em Junho de 2015

Francesco

June 30, 2015
Tweet

More Decks by Francesco

Other Decks in Programming

Transcript

  1. Analytics func myAnalyticsLogger<T>(value: T) { print(value) } Estado da Comida

    enum FoodState { case Raw, Fried, Cooked(Cooker) }
  2. Cooker enum Cooker : CookerType { case Oven, Microwave, Stove

    } CookerType protocol CookerType { func cook(Food) }
  3. Meu primeiro Cooker extension Cooker { func cook(food: Food) {

    food.state = .Cooked(self) myAnalyticsLogger("Just cooked a \(food.state)\ \(food.name) using a \(self)") } }
  4. Comida class Food { var state: FoodState var name: String

    init(name: String, state: FoodState) { self.name = name self.state = state myAnalyticsLogger("Just created a \(state) \(name)") } }
  5. Bacon class Bacon : Food { let tasty = true

    } Meu bacon let myBacon = Bacon(name: "!", state: .Raw) //Just created a FoodState.Raw !
  6. Enums · Reflection · Carregam representação textual · Either<T1, T2>

    funciona · Podem ser recursivos (no more Boxes) · indirect
  7. Enums recursivos Futuramente enum Tree<T> { case Leaf(T) inidirect case

    Node(Tree, Tree) } Pull Requests are welcome :)
  8. Escopos arbitrários do { let myTemporaryMicrowave = Cooker.Microwave var myTemporaryBacon

    = Bacon(name: "bacon", state: .Raw) myTemporaryMicrowave.cook(myTemporaryBacon) } //myTemporaryBacon não existe mais Consigo restringir mutabilidade
  9. Outras mudanças Antes: do {} while Agora: repeat{} while ·

    Options sets funcionam como tipos nativos!
  10. Melhorias compilador · Funções e métodos · Novos warnings ·

    var --> let · Ignorar resultado de método funcional
  11. Cooker Melhorado Guard statements + pattern matching! extension Cooker {

    func cook(food: Food) { guard case .Raw = food.state else { myAnalyticsLogger("Attempted to cook \(food.state)\ \(food.name) using \(self)") return } food.state = .Cooked(self) myAnalyticsLogger("Just cooked a \(food.state)\ \(food.name) using a \(self)") } }
  12. Pattern matching everywhere · guard case · if case ·

    for case Loops com filtro · for in... where filter
  13. Melhorando Cooker Usando protocol extensions extension CookerType { func cook(food:

    Food) { guard case .Raw = food.state else { myAnalyticsLogger("Attempted to cook \(food.state)\ \(food.name) using \(self)") return } food.state = .Cooked(self) myAnalyticsLogger("Just cooked a \(food.state)\ \(food.name) using a \(self)") } }
  14. Criando um forno mais complexo struct Oven : CookerType {

    var temperature : Float var heatOn : Bool func turnOn() { self.heatOn = true } func turnOff() { self.heatOn = false } }
  15. Criando um forno mais complexo (que funciona) struct Oven :

    CookerType { var temperature : Float var heatOn : Bool mutating func turnOn() { self.heatOn = true } mutating func turnOff() { self.heatOn = false } }
  16. struct Oven : CookerType { ... mutating func cook(food: Food)

    { self.turnOn() guard case .Raw = food.state else { myAnalyticsLogger("Attempted to cook \(food.state)\ \(food.name) using \(self)") return } food.state = .Cooked(self) myAnalyticsLogger("Just cooked a \(food.state) \(food.name) using a\ \(self) at \(temperature) degrees") self.turnOff() } }
  17. Defer ou como não colocar fogo na casa struct Oven

    : CookerType { ... mutating func cook(food: Food) { self.turnOn() defer { self.turnOff() } guard case .Raw = food.state else { myAnalyticsLogger("Attempted to cook \(food.state)\ \(food.name) using \(self)") return } food.state = .Cooked(self) myAnalyticsLogger("Just cooked a \(food.state) \(food.name) using a\ \(self) at \(temperature) degrees") } }
  18. Error handling · Erros triviais --> Optionals · Erros irrecuperáveis

    --> throw · Erros não devem ser ignorados · Chega de error:nil
  19. Error handling · try · do {} catch · try!

    · NSError conforms com ErrorType · leves