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

Swiftをキめると 気持ちいい!

Swiftをキめると 気持ちいい!

AKIBAswift #04での資料です

Takaaki Tanaka

July 26, 2016
Tweet

More Decks by Takaaki Tanaka

Other Decks in Technology

Transcript

  1. About Me • ాத ޹໌ (Takaaki Tanaka) • Ϋϥεϝιουגࣜձࣾ •

    @kongmingtrap • iOS Developer (Swift / Objective-C) • GyazSquare / GitHub
  2. NBQqBUNBQ public func map<U>( _ transform: @noescape (Wrapped) throws ->

    U ) rethrows -> U? { switch self { case .some(let y): return .some(try transform(y)) case .none: return .none } }
  3. &YBNQMF enum PokemonType: String { case Grass = "Grass" case

    Water = "Water" case Fire = "Fire" case Electric = "Electric" } ͋Δ࣌͜Μͳ&OVN͕͓Γ·ͨ͠ɻ
  4. 4FRVFODF let types = ["Grass", "Water", "Fire", "Electric"] let pokemonTypes

    = types.map { PokemonType(rawValue: $0) } [Optional PokemonType.Grass Optional PokemonType.Water Optional PokemonType.Fire Optional PokemonType.Electric ] ݁Ռ͕0QUJPOBMʹͳΔ
  5. 4FRVFODF let types = ["Grass", "Water", "Fire", "Electric"] let pokemonTypes

    = types.flatMap { PokemonType(rawValue: $0) } [PokemonType.Grass PokemonType.Water PokemonType.Fire PokemonType.Electric] ݁Ռ͕VOXSBQ͞ΕΔ
  6. 4FRVFODF let types = ["Grass", "Water", "Fire", "Ice"] let pokemonTypes

    = types.map { PokemonType(rawValue: $0) } [Optional PokemonType.Grass Optional PokemonType.Water Optional PokemonType.Fire nil] ۪௚ʹ݁ՌΛฦ͢
  7. 4FRVFODF let types = ["Grass", "Water", "Fire", "Ice"] let pokemonTypes

    = types.flatMap { PokemonType(rawValue: $0) } [PokemonType.Grass PokemonType.Water PokemonType.Fire] ݁Ռ͕VOXSBQ͞ΕɺOJM͕ଧͪফ͞ΕΔ
  8. $PMMFDUJPO let types1 = ["Electric", "Electric", "Fire"] let types2 =

    ["Water", "Ice"] let types3 = ["Fire", "Grass", "Rock", "Grass"] let types = [types1, types2, types3].map { $0 } [["Electric", "Electric", "Fire"], ["Water", "Ice"], ["Fire", "Grass", "Rock", "Grass"]]
  9. $PMMFDUJPO let types1 = ["Electric", "Electric", "Fire"] let types2 =

    ["Water", "Ice"] let types3 = ["Fire", "Grass", "Rock", "Grass"] let types = [types1, types2, types3].flatMap { $0 } ["Electric", "Electric", "Fire", "Water", "Ice", "Fire", "Grass", "Rock", "Grass"] ݁Ռ͕qBUͳ"SSBZʹͳΔ
  10. $PMMFDUJPO let types1 = ["Electric", "Electric", "Fire"] let types2 =

    ["Water", "Ice"] let types3 = ["Fire", "Grass", "Rock", "Grass"] let types = [types1, types2, types3].flatMap { $0 }.flatMap { PokemonType(rawValue: $0) } [PokemonType.Electric PokemonType.Electric PokemonType.Fire PokemonType.Water PokemonType.Fire PokemonType.Grass PokemonType.Grass] OJM͕ଧͪফ͞ΕΔͨΊɺཉ͍͠σʔλ͚ͩ࢒Δ
  11. 0QUJPOBM let value: String? = "Fire" let value1 = value.map

    { PokemonType(rawValue: $0) } Optional Optional PokemonType.Fire ݁Ռ͕ೋॏͷ0QUJPOBMʹͳΔ
  12. 0QUJPOBM let value: String? = "Fire" let value1 = value.flatMap

    { PokemonType(rawValue: $0) } Optional PokemonType.Fire ݁Ռͷ0QUJPOBM͕ଧͪফ͞ΕΔʢVOXSBQ͞ΕΔʣ
  13. 0QUJPOBM let type = PokemonType(rawValue: "Water") let changedType = pokemon.map

    { type -> PokemonType in switch type { case .Water: return PokemonType.Electric default: return type } } Optional PokemonType.Electric VOXSBQͯ͠஋ΛऔΓѻ͏
  14. 0QUJPOBM let value1: Int? = 10 let value2: Int? =

    20 let value = value1.flatMap { v1 in value2.map { v2 in v1 + v2 } } Optional 30 0QUJPOBMಉ࢜ͷ஋ͷܭࢉ