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

Mobile Programming Control Flow 1

Mobile Programming Control Flow 1

MobileProgrammingの授業で使うスライドです!

Avatar for yuichiro_takahashi

yuichiro_takahashi

October 10, 2018
Tweet

More Decks by yuichiro_takahashi

Other Decks in Technology

Transcript

  1. ςΩετ จʁࣜʁ(͓·͚) ▸ ࣜͱจͷҧ͍͸ ஋Λฦ͔͢൱͔Ͱ͢ ▸ ྫ͑͹ “ifจ” ͸஋Λฦ͞ͳ͍ͷͰӈͷྫͷΑ͏ͳ͜ͱ͸Ͱ͖ ·ͤΜ

    let message = if age > 18 { "੒ਓͰ͢" } else { "ະ੒೥Ͱ͢" } let message: String if age > 18 { message = "੒ਓͰ͢" } else { message = "ະ੒೥Ͱ͢" } จͷ৔߹ ࣜͷ৔߹
  2. ςΩετ IFจ ▸ ৚݅ʹΑͬͯॲཧΛ෼ذ͍ͤͨ͞ͱ͖ʹ͸ifΛ࢖͍·͢ ▸ จ๏͸ ▸ if <৚݅ࣜ> {

    /* ৚͕݅ਖ਼ͷ࣌ */ } else { /* ৚͕݅ෛͷ࣌ */ } ▸ ͱͳΓ·͢ ▸ ৚͕݅ෳ਺͋Δ৔߹͸ else if ΋࢖͑·͢
  3. ςΩετ ྫจ // جຊܗ let age = 20 if age

    >= 20 { print("੒ਓͯ͠·͢ʂ") } else { print("ະ੒೥Ͱ͢") } // ৚͕݅ෳ਺͋Δ৔߹ if age >= 18 { print("େֶੜͰ͢") } else if age >= 15 { print("ߴߍੜͰ͢") } else if age >= 12 { print("࣮͸ຐ଒ͩͬͨࢲ͸தֶੜ……গͳ͘ } else { print("খֶੜͰ͢") } // else͸লུՄೳ if age >= 20 { print("੒ਓͯ͠·͢ʂ") }
  4. ςΩετ IF-LET? ▸ Optionalܕ(Ex: Int?)͔Β஋Λ҆શʹऔΓग़͢ํ๏ ▸ Optional-binding(ΦϓγϣφϧόΠϯσΟϯά)ͷҰͭ ▸ if-let ࣜͱݺ͹ΕΔ

    ▸ ৚݅෼ذͱ͚ͯͩ͠Ͱͳͪ͘͜Β΋ඇৗʹΑ͘࢖ΘΕΔ ▸ จ๏͸ ▸ if let <ม਺໊> = <optional> { /* not nil */ } else { /* nil */ }
  5. ςΩετ ྫจ // جຊܗ let age: Int? = 20 if

    let a = age { print("๻͸ \(a)ࡀͰ͢ʂ") } else { print("೥ྸ͸͋Γ·ͤΜ") } // if-let + ৚݅෼ذ let age: Int? = 20 if let a = age, a >= 20 { print("๻͸ \(a)ࡀͰɺ੒ਓͯ͠·͢ʂ") }
  6. ςΩετ GUARDจ ▸ Swiftಠࣗͷߏจ ▸ จ๏͸ guard <৚݅ࣜ> else {

    /* returnͤ͞Δ */ } ▸ ifͱҧ͍৚݅ʹϚονͨ͠৔߹ॲཧΛਐΊɺϚον͠ͳ͍৔߹͸ else ͷϒϩοΫͷ தʹೖΓͦΕҎ্ॲཧΛਐ·ͤͳ͍Α͏ʹ͠·͢ ▸ else ϒϩοΫͷதʹ͸ ඞͣ return Λهड़͢Δඞཁ͕͋Γ·͢ɻ ▸ ৚݅ʹϚον͠ͳ͔ͬͨ৔߹ʹreturnͤ͞ΔΑ͏ʹॲཧΛॻ͘͜ͱΛGuardઅͱݴ͍ ·͢ ▸ Swiftʹ͸ guard ͱ͍͏ guardઅઐ༻ͷߏจ͕͋Γ·͕͢ଞͷݴޠͰ͸ if Λ࢖࣮ͬͯ ݱ͠·͢
  7. ςΩετ ྫจ func forAdult(age: Int) { guard age >= 18

    else { print("ݟͤΒΕͳ͍Αʂ") return // ͜ΕΛॻ͔ͳ͍ͱΤϥʔ } print("Welcome to Underground...") } func forAdult(age: Int) { if age < 18 { print("ݟͤΒΕͳ͍Αʂ") return } print("Welcome to Underground...") } ࠨͷೋͭ͸ಉ͡ॲཧΛͯ͠Δ
  8. ςΩετ GUARD-LET ▸ if-letͱಉ͘͡ guard-letࣜ΋͋Γ·͢ ▸ جຊతʹ͸guardͱಉ͡Ͱ͢ ▸ ҧ͍͸ nil

    ͩͬͨΒઌʹਐ·ͤͳ͍ͱ͍͏͜ͱ͘Β͍ ▸ จ๏͸ ▸ guard let <ม਺໊> = <Optional> else { /* return */ }
  9. ςΩετ ྫจ func parseResponse(responseData: [String: Any]?) -> String? { //

    check response guard let data = responseData else { print("response data is empty") return nil } guard let id = data["id"] as? String else { print("user id is not found.") return nil } return id } ωοτϫʔΫܦ༝Ͱऔಘͨ͠σʔλΛύʔε͢Δ
  10. ςΩετ SWITCHจ ▸ ifͱಉ͘͡৚݅෼ذ, optional-bindingΛ͢Δͱ͖ʹ࢖͏ ▸ ݹ͍ݴޠͩͱ Int΍FloatͳͲ͔͠࢖͑ͳ͍͕SwiftͰ͸جຊత ʹԿͰ΋Ϛονϯάͤ͞ΒΕΔ ▸

    breakΛॻ͔ͳͯ͘΋ॲཧ͕࣍ʹҠΒͳ͍(fall-through͠ͳ͍) ▸ ৚݅ʹ߹கͤ͞Δ͜ͱΛύλʔϯϚονͱݺΜͩΓ͢Δ ▸ ΊͪΌͪ͘Όศར
  11. ςΩετ SWITCHจ ▸ จ๏͸ switch <஋> { case <৚݅ࣜ1>: /*

    ॲཧ1 */ case <৚݅ࣜ2>: /* ॲཧ2 */ case <৚݅ࣜ3>: /* ॲཧ3 */ default: /* ͦΕҎ֎ */ }
  12. ςΩετ ஋Ϛονϯά ▸ γϯϓϧʹಛఆͷ஋Λࢦఆͯ͠Ϛονϯάͤ͞Δํ๏ ▸ Intͷ৔߹͸஋͕ 10ͩͬͨΒ, 20ͩͬͨΒͱ͍͏ܗ ▸ ΧϯϚͰ۠੾ͬͯෳ਺ͷ஋Λฒ΂Δ͜ͱ΋Ͱ͖Δ

    let age = 20 switch age { case 20: print("੒ਓͰ͢") case 13, 14, 15: print("தֶੜͰ͢") case 12: print("খֶੜͰ͢ʂ") default: print("όϒʔʂ") }
  13. ςΩετ ൣғϚονϯά ▸ ԋࢉࢠͷͱ͖ʹར༻ͨ͠ ൣғԋࢉࢠΛར༻ͯ͠Ϛονϯάͤ͞Δ͜ͱ΋Մೳ ▸ Intͷ৔߹͸஋͕ 10~20ͩͬͨΒͱ͍͏ܗ let age

    = 20 switch age { case 16...18: print("੒ਓͰ͢") case 13..<16: print("தֶੜͰ͢") case 6..<13: print("খֶੜͰ͢ʂ") default: print("όϒʔʂ") }
  14. ςΩετ λϓϧ(TUPLE)ʁ ▸ SwiftͰ࢖ΘΕΔσʔλߏ଄ͷҰͭ ▸ ΧϯϚͰ۠੾ΒΕͨϦετ ▸ Arrayͱ͸ҧ͍ෳ਺ͷܕΛ࣋ͭ͜ͱ͕Ͱ͖Δ ▸ ཁૉΛ࣋ͯΔ্ݶ͕ܾ·͍ͬͯΔͨΊɺArrayͷΑ͏ʹ͸࢖Θͳ͍

    ▸ ↓ҎԼͷΑ͏ͳײ͡ ▸ let tuple = (10, “yuichiro”, 34.5, [“gas”, “gas”, “gas”]) ▸ ઌ಄ͷཁૉ͔Β tuple.0, tuple.1 ͱ͍͏ܗͰΞΫηε͢Δ ▸ ཁૉʹ໊લ(ϥϕϧ)Λ͚ͭΔ͜ͱ΋Մೳ(໊લ෇͖λϓϧ) ▸ let tuple = (number: 10, string: “yuichiro”, float: 34.5, array: [“gas”, “gas”, “gas”])
  15. ςΩετ λϓϧϚονϯά ▸ ઌ΄Ͳઆ໌ͨ͠TupleΛར༻ͯ͠Ϛονϯάͤ͞Δ͜ͱ΋Մೳ ▸ tupleͷཁૉͷ਺΍஋ͰϚονϯάͤ͞Δ switch user { case

    let (age, name): print("user is age: \(age), name: \(name)") case (let age, "hogefuga"): print("user is age: \(age), name: hogefuga") case (20, let name): print("user is age: 20, name: \(name)") case (20, "hogefuga"): print("user is age: 20, name: hogefuga") case let (age, name, gender): print(“Ϛον͠·ͤΜ") default: print("something else") }
  16. ςΩετ ENUMʁ ▸ SwiftͰ࢖ΘΕΔσʔλߏ଄ͷҰͭ ▸ ෳ਺ͷ஋Λάϧʔϐϯά͓ͯ͘͜͠ͱ͕Ͱ͖Δܕ ▸ ೔ຊޠͰ͸ྻڍܕͱ͍͏ ▸ จ๏͸

    enum <ྻڍ໊> { case <ྻڍࢠ1>, case <ྻڍࢠ2>, … } ▸ Swiftͷྻڍܕ΋ଞͷݴޠʹൺ΂Δͱศར ▸ Optionalܕ΋࣮͸Ϋϥε͡Όͳͯ͘enumͰ͢ ▸ ৄ͘͠͸ޙ΄Ͳษڧ͠·͢
  17. ENUMϚονϯά enum NetworkError { case authError case unexpectedData case unknownError

    case nilResponse } switch error { case .authError: print("ೝূʹࣦഊ͠·ͨ͠") case .unexpectedData: print("ෆਖ਼ͳσʔλͰ͢") case .unknownError: print("Α͘Θ͔Γ·ͤΜ͕ΤϥʔͰ͢(^q^)") case .nilResponse: print("Ϩεϙϯε͕ۭͬΆͰ͢") case .timeout: print("αʔόʔ͔ΒԠ౴͕͋Γ·ͤΜ") }
  18. ΋͏Ұ౓ enum NetworkError { case authError case unexpectedData case unknownError

    case nilResponse } switch error { case .authError: print("ೝূʹࣦഊ͠·ͨ͠") case .unexpectedData: print("ෆਖ਼ͳσʔλͰ͢") case .unknownError: print("Α͘Θ͔Γ·ͤΜ͕ΤϥʔͰ͢(^q^)") case .nilResponse: print("Ϩεϙϯε͕ۭͬΆͰ͢") case .timeout: print("αʔόʔ͔ΒԠ౴͕͋Γ·ͤΜ") }
  19. ΋͏Ұ౓ enum NetworkError { case authError case unexpectedData case unknownError

    case nilResponse } switch error { case .authError: print("ೝূʹࣦഊ͠·ͨ͠") case .unexpectedData: print("ෆਖ਼ͳσʔλͰ͢") case .unknownError: print("Α͘Θ͔Γ·ͤΜ͕ΤϥʔͰ͢(^q^)") case .nilResponse: print("Ϩεϙϯε͕ۭͬΆͰ͢") case .timeout: print("αʔόʔ͔ΒԠ౴͕͋Γ·ͤΜ") } default͕ͳ͍ʂ
  20. ςΩετ OPTIONALϚονϯά ▸ Optionalܕ(?ܕ)Λ࢖༻ͯ͠Ϛονϯάͤ͞Δ ▸ ܗ͸enumʹࣅ͍ͯΔ ▸ ཁૉ͸ some(value) ͔

    none ͷΈ ▸ ஋͕͋ͬͨͱ͖ɺແ͔ͬͨͱ͖Ͱ໌֬ʹॲཧΛ෼͚͍ͨ৔߹͸if-letΑΓ΋ͪ͜ΒΛਪ঑ let brain: Brain? = nil switch brain { case .some(let brain): print("ਖ਼ৗͰ͢") case .none: print("಄ۭͬΆʂ") }
  21. ςΩετ TYPE-CASTϚονϯά ▸ ܕม׵Λ࢖͏΋͘͠͸ಛఆͷܕΛࢦఆͯ͠Ϛονϯάͤ͞Δ΍Γํ ▸ ܕม׵Λ࢖͏ࡍ͸ “as", ܕΛࢦఆ͍ͨ͠৔߹͸ “is” Λ࢖͏

    ▸ Ͱ͖Ε͹ܕม׵Λߦ͏ίʔυ͕ͳ͍ͷ͕ཧ૝Ͱ͸͋ΔͷͰ࢖͏͜ͱ͸ গͳ͍(͔΋?) let nyantyu: Animal = Cat(name: "nantyu") switch nyantyu { case let a as Cat: print("ೣͰͨ͠(?)") a.burk() case is Dog: print("ݘͰ͢") default: print("ಈ෺Ͱ͸͋Γ·ͤΜ") }