Share
Embed
Transcript
34 slides
-
ςΩετ ྻڍܕ(ENUM)ͱʁ ▸ ؔ࿈͢ΔΛҰͭͷܕͱͯ͠·ͱΊͨͷ ▸ ྫ͑ϙέϞϯͷλΠϓͳͲྻڍܕ(enum)Ͱද͢͜ͱ͕Ͱ ͖Δ ▸ λΠϓͰॲཧΛ͚͍ͨ߹จࣈྻࣈΛ͏ΑΓenum Λ͏ํ͕ϛεΛແ͘͘͢͠ɺ҆શʹ࣮͢Δ͜ͱ͕Ͱ͖Δ
▸ จ๏enum <EnumName> { case member1 case member2 } -
ςΩετ ࣮ࡍͷίʔυ(ENUMͷఆٛ) // ϙέϞϯͷλΠϓΛEnumͰ·ͱΊͨ߹ enum PokemonType { case fire //
΄ͷ͓λΠϓ case grass // ͘͞λΠϓ case water // ΈͣλΠϓ case normal // ϊʔϚϧλΠϓ } -
ςΩετ ࣮ࡍͷίʔυ(มʹೖΕΔ) // ϙέϞϯͷλΠϓΛEnumͰ·ͱΊͨ߹ enum PokemonType { case fire //
΄ͷ͓λΠϓ case grass // ͘͞λΠϓ case water // ΈͣλΠϓ case normal // ϊʔϚϧλΠϓ } var pokemonType: PokemonType pokemonType = PokemonType.fire // جຊܕ pokemonType = .grass // ܕͷ໊લলུՄೳ -
ςΩετ ࣮ࡍͷίʔυ(ϓϩύςΟϝιουఆٛͰ͖·͢) 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()) // ϝιουݺͼग़͠ -
ςΩετ ENUMͷಛ ▸ switch(NintendoͰͳ͍)Ͱ enum Λ͏߹defaultΛॻ͔ ͳͯ͘͢Ή(ཏνΣοΫ) ▸ ϝϯόʔ͕Λ࣋ͨͤΔ͜ͱ͕Ͱ͖Δ (ܕenum)
▸ ϝϯόʔʹؔ࿈͢ΔܕΛ͚ͬͭ͘Δ͜ͱ͕Ͱ͖Δ (ؔ࿈ܕ enum) -
ςΩετ ཏνΣοΫ? ▸ enum Ͱ switchͰ݅ذΛߦ͏ࡍʹνΣοΫ͕ೖΔ ▸ ͦͷenumͷϝϯό͕શͯྻڍ͞Ε͍ͯΔ͔ΛνΣοΫͯ͠ Γͳ͍߹ΤϥʔΛग़͢(ίϯύΠϧΤϥʔ) ▸
ྻڍܕͷϝϯό͕ܾ·͍ͬͯΔͷͰ͜ͷΑ͏ͳνΣοΫ ͕Մೳ ▸ ΤϥʔΛղফ͢ΔʹશͯͷϝϯόΛྻڍ͢Δ͔ default Λ هड़͢Δ -
ςΩετ ࣮ࡍͷίʔυ(͜ͷΑ͏ͳENUM͕͋ͬͨ߹) // ϙέϞϯͷλΠϓΛEnumͰ·ͱΊͨ߹ enum PokemonType { case fire //
΄ͷ͓λΠϓ case grass // ͘͞λΠϓ case water // ΈͣλΠϓ case normal // ϊʔϚϧλΠϓ } -
ςΩετ ࣮ࡍͷίʔυ(͜Μͳ෩ʹνΣοΫ͕͞Ε·͢) // ͬͪ͜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 "ϊʔϚϧ" } } -
ςΩετ ܕENUM? ▸ enumͷϝϯόʹInt, Float(or Double), String ͷΛηοτͨ͠ Enumͷ͜ͱ ▸
จ๏ enum <EnumName>: Type { case member1 = <value> … } ▸ ϝϯόʹ༩͑ΒΕͨ variable.rawValue ͱ͍͏ܗͰऔಘ͕Մೳ ▸ ·ͨ EnumName(rawValue: <value>) ͱ͍͏ܗͰenumͷऔಘ͕ Մೳ -
ςΩετ ࣮ࡍͷίʔυ(STRINGΛͬͨ߹) // ϙέϞϯͷλΠϓʹจࣈΛՃ enum PokemonType: String { case fire
= "΄ͷ͓" case grass = "͘͞" case water = "Έͣ" case normal = "ϊʔϚϧ" } var pokemonType = PokemonType(rawValue: "΄ͷ͓") ?? .normal // enumΛ࡞ print(pokemonType.rawValue) -
ςΩετ ؔ࿈ܕENUM? ▸ Enumͷϝϯόʹରͯؔ͠࿈͢ΔܕΛࢦఆ͢Δ͜ͱͷͰ͖Δ ػೳ ▸ ܕͷEnumͱҧ͍ϝϯόʹܕ͚ͩΛࢦఆ͢Δ ▸ จ๏ enum
<EnumName>: Type { case member1(Type) … } ▸ ͲΜͳঢ়ଶ͔ͱ͍͏͜ͱ͚ͩͰͳͦ͘Εʹਵ͢ΔՃใ Λ͚ͬͭ͘Δ͜ͱ͕Ͱ͖Δ -
ςΩετ ۩ମྫ func formattedPostDateTime(datetime: Int) -> String { // ࣮...
} // ҾʹͲΕ͘Β͍લ͔࣌ؒΛ༩͑ͯϑΥʔϚοτ formattedPostDateTime(datetime: 10) -
ςΩετ ۩ମྫ func formattedPostDateTime(datetime: Int) -> String { // ࣮...
} // ҾʹͲΕ͘Β͍લ͔࣌ؒΛ༩͑ͯϑΥʔϚοτ formattedPostDateTime(datetime: 10) // <- "10" 10લʁ 10࣌ؒ લʁ 10લʁ 10ඵલʁ -
ςΩετ ͜ΕͰ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) -
ςΩετ ͜ΕͰ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࣌ؒલʹͳΔʁ -
ςΩετ ͜ΕͰ͍͍͚Ͳ…? // ࣌ؒͷ୯ҐΛද͢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) -
ςΩετ ؔ࿈ܕ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)) -
ςΩετ 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) -
ςΩετ 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))