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

Mobile Programming enum

Mobile Programming enum

モバイルプログラミングの授業で使うスライドです

yuichiro_takahashi

December 19, 2018
Tweet

More Decks by yuichiro_takahashi

Other Decks in Technology

Transcript

  1. ςΩετ ࣮ࡍͷίʔυ(ENUMͷఆٛ) // ϙέϞϯͷλΠϓΛEnumͰ·ͱΊͨ৔߹ enum PokemonType { case fire //

    ΄ͷ͓λΠϓ case grass // ͘͞λΠϓ case water // ΈͣλΠϓ case normal // ϊʔϚϧλΠϓ }
  2. ςΩετ ࣮ࡍͷίʔυ(ม਺ʹೖΕΔ) // ϙέϞϯͷλΠϓΛEnumͰ·ͱΊͨ৔߹ enum PokemonType { case fire //

    ΄ͷ͓λΠϓ case grass // ͘͞λΠϓ case water // ΈͣλΠϓ case normal // ϊʔϚϧλΠϓ } var pokemonType: PokemonType pokemonType = PokemonType.fire // جຊܕ pokemonType = .grass // ܕͷ໊લ͸লུՄೳ
  3. ςΩετ ࣮ࡍͷίʔυ(ϓϩύςΟ΍ϝιου΋ఆٛͰ͖·͢) enum PokemonType { // লུ… func getTypeString() ->

    String { switch self { case .fire: return "΄ͷ͓" case .grass: return "͘͞" case .water: return "Έͣ" case .normal: return "ϊʔϚϧ" } } } var pokemonType: PokemonType = .fire print(pokemonType.getTypeString()) // ϝιουݺͼग़͠
  4. ςΩετ ໢ཏνΣοΫ? ▸ enum Ͱ switchͰ৚݅෼ذΛߦ͏ࡍʹνΣοΫ͕ೖΔ ▸ ͦͷenumͷϝϯό͕શͯྻڍ͞Ε͍ͯΔ͔ΛνΣοΫͯ͠ ଍Γͳ͍৔߹͸ΤϥʔΛग़͢(ίϯύΠϧΤϥʔ) ▸

    ྻڍܕͷϝϯό͸਺͕ܾ·͍ͬͯΔͷͰ͜ͷΑ͏ͳνΣοΫ ͕Մೳ ▸ ΤϥʔΛղফ͢Δʹ͸શͯͷϝϯόΛྻڍ͢Δ͔ default Λ هड़͢Δ
  5. ςΩετ ࣮ࡍͷίʔυ(͜ͷΑ͏ͳENUM͕͋ͬͨ৔߹) // ϙέϞϯͷλΠϓΛEnumͰ·ͱΊͨ৔߹ enum PokemonType { case fire //

    ΄ͷ͓λΠϓ case grass // ͘͞λΠϓ case water // ΈͣλΠϓ case normal // ϊʔϚϧλΠϓ }
  6. ςΩετ ࣮ࡍͷίʔυ(͜Μͳ෩ʹνΣοΫ͕͞Ε·͢) // ͬͪ͜͸OK(ϝϯό͕શͯྻڍ͞Ε͍ͯΔ) func getTypeString() -> String { switch

    self { case .fire: return "΄ͷ͓" case .grass: return "͘͞" case .water: return "Έͣ" case .normal: return "ϊʔϚϧ" } } // ͬͪ͜͸NG(ϝϯό͕શͯྻڍ͞Ε͍ͯͳ͍) func getTypeString() -> String { switch self { case .fire: return "΄ͷ͓" case .grass: return "͘͞" case .water: return "Έͣ" } } // ͬͪ͜΋OK(ϝϯό͕શͯྻڍ͞Ε͍͍ͯͳ͍͕default͕͋Δ) func getTypeString() -> String { switch self { case .fire: return "΄ͷ͓" case .grass: return "͘͞" default: return “ϊʔϚϧ" } } // ͬͪ͜΋NG(default͕༨ܭ) func getTypeString() -> String { switch self { case .fire: return "΄ͷ͓" case .grass: return "͘͞" case .water: return "Έͣ" case .normal: return “ϊʔϚϧ" default: return "ϊʔϚϧ" } }
  7. ςΩετ ஋ܕENUM? ▸ enumͷϝϯόʹInt, Float(or Double), String ͷ஋Ληοτͨ͠ Enumͷ͜ͱ ▸

    จ๏͸ enum <EnumName>: Type { case member1 = <value> … } ▸ ϝϯόʹ༩͑ΒΕͨ஋͸ variable.rawValue ͱ͍͏ܗͰऔಘ͕Մೳ ▸ ·ͨ EnumName(rawValue: <value>) ͱ͍͏ܗͰenumͷऔಘ͕ Մೳ
  8. ςΩετ ࣮ࡍͷίʔυ(STRINGΛ࢖ͬͨ৔߹) // ϙέϞϯͷλΠϓʹจࣈΛ௥Ճ enum PokemonType: String { case fire

    = "΄ͷ͓" case grass = "͘͞" case water = "Έͣ" case normal = "ϊʔϚϧ" } var pokemonType = PokemonType(rawValue: "΄ͷ͓") ?? .normal // enumΛ࡞੒ print(pokemonType.rawValue)
  9. ςΩετ ؔ࿈ܕENUM? ▸ Enumͷϝϯόʹରͯؔ͠࿈͢ΔܕΛࢦఆ͢Δ͜ͱͷͰ͖Δ ػೳ ▸ ஋ܕͷEnumͱ͸ҧ͍ϝϯόʹܕ͚ͩΛࢦఆ͢Δ ▸ จ๏͸ enum

    <EnumName>: Type { case member1(Type) … } ▸ ͲΜͳঢ়ଶ͔ͱ͍͏͜ͱ͚ͩͰͳͦ͘Εʹ෇ਵ͢Δ෇Ճ৘ใ Λ͚ͬͭ͘Δ͜ͱ͕Ͱ͖Δ
  10. ςΩετ ۩ମྫ func formattedPostDateTime(datetime: Int) -> String { // ࣮૷...

    } // Ҿ਺ʹͲΕ͘Β͍લ͔࣌ؒΛ༩͑ͯϑΥʔϚοτ formattedPostDateTime(datetime: 10)
  11. ςΩετ ۩ମྫ func formattedPostDateTime(datetime: Int) -> String { // ࣮૷...

    } // Ҿ਺ʹͲΕ͘Β͍લ͔࣌ؒΛ༩͑ͯϑΥʔϚοτ formattedPostDateTime(datetime: 10) // <- "10" ͸ 10೔લʁ 10࣌ؒ લʁ 10෼લʁ 10ඵલʁ
  12. ςΩετ ͜ΕͰOK…? // Ҿ਺ʹ࣌ؒͷ୯ҐΛ௥Ճ func formattedPostDateTime(datetime: Int, termType: Int) ->

    String { switch termType { case 0: return "\(datetime)೔લ" case 1: return "\(datetime)࣌ؒલ" // লུ... default: return "গ͠લ" } }
 // Ҿ਺ʹͲΕ͘Β͍લ͔࣌ؒΛ༩͑ͯϑΥʔϚοτ formattedPostDateTime(datetime: 10, termType: 0)
  13. ςΩετ ͜ΕͰOK…? // Ҿ਺ʹ࣌ؒͷ୯ҐΛ௥Ճ func formattedPostDateTime(datetime: Int, termType: Int) ->

    String { switch termType { case 0: return "\(datetime)೔લ" case 1: return "\(datetime)࣌ؒલ" // লུ... default: return "গ͠લ" } }
 // Ҿ਺ʹͲΕ͘Β͍લ͔࣌ؒΛ༩͑ͯϑΥʔϚοτ formattedPostDateTime(datetime: 10, termType: 0) // <- 0 ͩͱ n೔લʹͳΔΜ͚ͩͬʁ n࣌ؒલʹͳΔʁ
  14. ςΩετ ͜ΕͰ΋͍͍͚Ͳ…? // ࣌ؒͷ୯ҐΛද͢EnumΛ௥Ճ enum DateTerm { case day case

    hours case minutes case seconds } // Ҿ਺ʹ౉࣌ؒ͢ͷ୯ҐΛInt͔ΒDateTermʹมߋ func formattedPostDateTime(datetime: Int, termType: DateTerm) -> String { switch termType { case .day: return "\(datetime)೔લ" case .hours: return "\(datetime)࣌ؒલ" // লུ... } } // Ҿ਺ʹͲΕ͘Β͍લ͔IntͱDateTermΛ༩͑ͯϑΥʔϚοτ formattedPostDateTime(datetime: 10, termType: .day)
  15. ςΩετ ؔ࿈ܕENUMΛ࢖͑͹ɾɾɾ // DateTermͷϝϯόʹؔ࿈͢Δܕ(ࠓճ͸Int)Λ௥Ճ enum DateTerm { case day(Int) case

    hours(Int) case minutes(Int) case seconds(Int) } // Ҿ਺ΛDateTermҰͭʹ·ͱΊΒΕͨʂ func formattedPostDateTime(from term: DateTerm) -> String { switch term { case .day(let value): return “\(value)೔લ" // <- value ʹ͸10͕ೖ͍ͬͯΔ case .hours(let value): return "\(value)࣌ؒલ" // লུ… } } // 10೔લͱϑΥʔϚοτ formattedPostDateTime(from: .day(10))
  16. ςΩετ BEFORE // Ҿ਺ʹ࣌ؒͷ୯ҐΛ௥Ճ func formattedPostDateTime(datetime: Int, termType: Int) ->

    String { switch termType { case 0: return "\(datetime)೔લ" case 1: return "\(datetime)࣌ؒલ" // লུ... default: return "গ͠લ" } }
 // Ҿ਺ʹͲΕ͘Β͍લ͔࣌ؒΛ༩͑ͯϑΥʔϚοτ formattedPostDateTime(datetime: 10, termType: 0)
  17. ςΩετ AFTER enum DateTerm { case day(Int) case hours(Int) case

    minutes(Int) case seconds(Int) } func formattedPostDateTime(from term: DateTerm) -> String { switch term { case .day(let value): return “\(value)೔લ" case .hours(let value): return "\(value)࣌ؒલ" // লུ… } } formattedPostDateTime(from: .day(10))