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

OptionalSwift

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

 OptionalSwift

About Optional at Swift

Avatar for Lee Geunil

Lee Geunil

January 30, 2017
Tweet

Other Decks in Programming

Transcript

  1. Swift には ! と ? 沢山出てくる! var bool: Bool? =

    true var str: String! = "hoge" if !bool! { (str != nil) ? "true!" : "false!" } else { print(str?.uppercased() ?? "this is nil") }
  2. Swift には ! と ? 沢山出てくる! var bool: Bool? =

    true var str: String! = "hoge" if !bool! { (str != nil) ? "true!" : "false!" } else { print(str?.uppercased() ?? "this is nil") } Swift 初心者:
  3. なぜオプショナルが必要なのか? Objective-C では普通にnil を代入出来る。 NSString *hoge = nil; label.text(hoge); //

    落ちない nil の変数を実行しても落ちない。 意図しないnil で重大なエラー になりかねない。
  4. なぜオプショナルが必要なのか? Objective-C では普通にnil を代入出来る。 NSString *hoge = nil; label.text(hoge); //

    落ちない nil の変数を実行しても落ちない。 意図しないnil で重大なエラー になりかねない。 if ( hoge != nil ) { // 要チェック label.text(hoge); }
  5. なぜオプショナルが必要なのか? Objective-C では普通にnil を代入出来る。 NSString *hoge = nil; label.text(hoge); //

    落ちない nil の変数を実行しても落ちない。 意図しないnil で重大なエラー になりかねない。 ちなみにjava の場合はNullPointerException で落ちる ※ コンパイルエラー にならない ※Java8 からはOptional がある
  6. なぜオプショナルが必要なのか? Swift では変数にnil かどうかをオプショナルでわか るようになったので安全である。 var hoge: String? = nil

    button.text = hoge // コンパイルエラー!! コンパイルエラー になるので、 テスト漏れやリリー ス する前にバグを発見出来るので安全!!
  7. アンラップのやり方 Forced Unwrapping ( 要注意) Optional Chaining( 手抜き) Optional Binding(

    可能な限りこれを使う) 個人的な主観ですが・・・
  8. Forced Unwrapping nil の場合 落ちます! var hoge: String? = nil

    print(hoge!.uppercased()) 今回の”!” をほっとかないの意味は Forced Unwrapping の事です!
  9. Forced Unwrapping nil の場合 落ちます! var hoge: String? = nil

    print(hoge!.uppercased()) 今回の”!” をほっとかないの意味は Forced Unwrapping の事です! 確実にnil ではない場合以外でForced Unwrapping を使 ってはいけません。
  10. Forced Unwrapping nil の場合 落ちます! var hoge: String? = nil

    print(hoge!.uppercased()) 今回の”!” をほっとかないの意味は Forced Unwrapping の事です! 確実にnil ではない場合以外でForced Unwrapping を使 ってはいけません。 次に紹介するOptional Chaining を使いましょう!
  11. Optional Chaining nil の場合でも落ちません var hoge: String? = nil print(hoge?.uppercased())

    hoge がnil の場合処理を中断してくれます。 気楽にアンラッピング出来て楽ですが、 nil の場合の処理が書けません。
  12. Optional Chaining nil の場合でも落ちません var hoge: String? = nil print(hoge?.uppercased())

    hoge がnil の場合処理を中断してくれます。 気楽にアンラッピング出来て楽ですが、 nil の場合の処理が書けません。 次に紹介する Optional Binding を使いましょう!
  13. Optional Binding if let を使ったやり方 var hoge: String? = "Hello

    World!" if let hoge = hoge { print(hoge) // Hello World! } else { print("This is nil") }
  14. Optional Binding if let を使ったやり方 var hoge: String? = "Hello

    World!" if let hoge = hoge { print(hoge) // Hello World! } else { print("This is nil") } 一度に複数記述出来る var hoge: String? = "Hello " var fuga: String? = "World!" if let hoge = hoge, let fuga = fuga { print(hoge + fuga) // Hello World! }
  15. Optional Binding guard let を使ったやり方 var hoge: String? = "Hello

    World!" guard let hoge = hoge else { print("This is nil") return } print(hoge) // Hello World!
  16. Optional Binding guard let を使ったやり方 var hoge: String? = "Hello

    World!" guard let hoge = hoge else { print("This is nil") return // return がない場合、 コンパイルエラー } print(hoge) // Hello World! return の記述が必須なので安全
  17. Optional Binding guard let を使ったやり方 var hoge: String? = "Hello

    World!" guard let hoge = hoge else { print("This is nil") return // return がない場合、 コンパイルエラー } print(hoge) // Hello World! return の記述が必須なので安全 guard の方が早期リター ン出来てネストを浅く出来て 良い。
  18. オプショナルの実態とは? 実はオプショナルの? は Optinal<T> のシンタックスシュガー で var hoge: String? var

    hoge: Optional<String> 上記2つは同じ意味です。 さらに詳細はSwift の中身を見るとわかります。 ソー スコー ドはgithub にあります。 swift -> stdlib -> public -> core -> Optional.swift
  19. オプショナルの実態はEnum である! この書き方は var hoge: String? = "Hello World!" var

    age: Int? = nil つまり var hoge: Optional<String> = .some("Hello World!") var age: Optional<Int> = .none ということ。
  20. オプショナルの実態はEnum である! Enum なのでSwitch 文でも判定出来る var hoge: String? = "Hello

    World!" if let hoge = hoge { print(hoge) // Hello World! } else { print("unknown") } var hoge: Optional<String> = .some("Hello World!") switch hoge { case .some(let stringValue): return print(hoge) // Hello World! case .none: return print("unknown") }
  21. オプショナルの?? 演算子 ?? は値が nil だった場合に後ろの値を使用する var hoge: String? =

    nil print(hoge ?? "this is nil") // this is nil 三項演算子のシンタックスシュガー var hoge: String? = nil print(hoge != nil ? hoge : "this is nil")
  22. オプショナルの?? 演算子の中身 public func ?? <T>(optional: T?, defaultValue: @autoclosure rethrows

    -> T? { switch optional { case .some(let value): return value case .none: return try defaultValue() } }