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

今日から使える! Optionalをいい感じに扱うtips

今日から使える! Optionalをいい感じに扱うtips

PKPK-Carnage

March 19, 2019
Tweet

More Decks by PKPK-Carnage

Other Decks in Programming

Transcript

  1. About me • iOSྺ2೥ͪΐͬͱ • Qiita → @PKPK-Carnage • Github

    → PKPK-Carnage • Twitter → @PKPK-Carnage
  2. switch 6 enum TestType: Int { case hoge = 0

    case fuga = 1 case piyo = 2 }
  3. switch 7 func generateText(from index: Int) -> String { guard

    let unwrappedType = TestType(rawValue: index) else { return "" } switch unwrappedType { case .hoge: return "Hoge" case .fuga: return "Fuga" case .piyo: return "Piyo" } }
  4. switch 9 func generateText(from index: Int) -> String { guard

    let unwrappedType = TestType(rawValue: index) else { return "" } switch unwrappedType { case .hoge: return "Hoge" case .fuga: return "Fuga" case .piyo: return "Piyo" } }
  5. switch 10 func generateText(from index: Int) -> String { switch

    TestType(rawValue: index) { case .hoge?: return "Hoge" case .fuga?: return "Fuga" case .piyo?: return "Piyo" case .none: return "" } }
  6. Nil Coalescing Operator 13 let text = "" let intValue

    = Int(text) == nil ? 0 : Int(text)!
  7. Nil Coalescing Operator + Closure 18 let text = ""

    let intValue = Int(text) ?? { return 0 }()
  8. ׆༻๏ 21 let text = "" let intValue = Int(text)

    ?? { assertionFailure("਺ࣈͷςΩετ͕ೖͬͯ·ͤΜʂ") return 0 }()