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

いいから `!` を使え! / Shut up and use `!` !

Af64bc38c0ffcfcabdf430759ee491ce?s=47 Elvis Shi
February 26, 2021

いいから `!` を使え! / Shut up and use `!` !

Af64bc38c0ffcfcabdf430759ee491ce?s=128

Elvis Shi

February 26, 2021
Tweet

Transcript

  1. ͍͍͔Β!Λ࢖͑ʂ f o r  : 6 . & .

    *  T X J G U    
  2. } var employedBy = "YUMEMI Inc." var job = "iOS

    Tech Lead" var favoriteLanguage = "Swift" var twitter = "@lovee" var qiita = "lovee" var github = "el-hoshino" var additionalInfo = """ อޢͨ͠ೣ͕ͷൃ৘ظʹಥೖͯ݁͠ߏେม """ final class Me: Developable, Talkable {
  3. 4XJGUͰ!࢖ͬͯ·͢ʁ

  4. ΋ͪΖΜBool൓సҎ֎Ͱ

  5. 4XJGUʹ͓͚Δ!ͷओͳ࢖͍ಓ˞ w Optionalͷڧ੍6OXSBQʢ*60ؚΉʣ w ܕม׵ͷڧ੍μ΢ϯΩϟετas! w ྫ֎ॲཧͷڧ੍ྫ֎ແࢹtry! ˞#PPM൓సҎ֎

  6. ڞ௨఺ɿϓϩάϥϜ͕ 
 Ϋϥογϡ͢ΔϦεΫ͕͋Δ

  7. !Λ࢖Θͳ͍ํ͕҆શ 本当にそうか?

  8. ͜ͷίʔυͬͯɺ҆શʁ func doSomething() { let image = UIImage(named: "ImageName") ??

    UIImage() // do something with image }
  9. ͜ͷίʔυͬͯɺ҆શʁ func doSomething() { guard let appDelegate = UIApplication .shared.delegate

    as? AppDelegate else { return } // do something with appDelegate }
  10. ͜ͷίʔυͬͯɺ҆શʁ func doSomething(with data: Data) { do { let decoder

    = JSONDecoder() let response = try decoder.decode(Response.self, from: data) // do something with response } catch { debugPrint(error) return } }
  11. ΞϓϦ͕Ϋϥογϡ͠ͳ͍͔Βɺ 
 ҆શɻ

  12. ΞϓϦ͕Ϋϥογϡ͠ͳ͍͔Βɺ 
 ҆શʜͳͷ͔ʁ

  13. ͦ΋ͦ΋ɺ҆શͬͯɺͳʹʁ

  14. ੲɺ0CK$͔Β4XJGUʹҠߦͨ࣌͠ͷ࿩˞ w αʔό͔Βը૾σʔλͱදࣔαΠζΛऔಘ͢Δॲཧ͕͋ͬͨ w αΠζσʔλ͸෯ߴͦ͞ΕͧΕ0CK$ͷίʔυͰintValueͰऔಘͯͨ͠ w 4XJGUԽ͢Δͱ͖͸Կ΋ߟ͑ͣʹas? Int ?? 0Ͱॲཧͯͨ͠

    w ϦϦʔεޙɺҰ෦ͷը૾ͷΈΞεϖΫτൺ͕มΘͬͯ͠·ͬͨࣄ͕݅ൃੜ w ௐࠪͨ͠Βɺαʔό͔Βؼ͖ͬͯͨσʔλ͕ͳ͔ͥจࣈྻσʔλͩͬͨͨ Ίɺ0CK$͸ࣗಈม׵ͯ͘͠Ε͕ͨ4XJGU͸ܕʹݫ͍ͨ͠Ίࣗಈม׵ͯ͘͠ Εͣɺ?? 0ʹΑͬͯ0͕୅ೖ͞Εͨ w ෯0ߴ͞0ͷ৔߹͸σϑΥϧταΠζͰදࣔ͢Δॲཧ͕͋ͬͨͨΊɺςε τσʔλͰಈ࡞֬ೝ͢Δͱ͖͸શ͘ؾ͔ͮͳ͔ͬͨ ˞࣮࿩Ͱ͢
  15. Ϋϥογϡ͸͠ͳ͔͕ͬͨɺ 
 !Λආ͚Δ͋·Γɺ 
 ٯʹόάͷൃݟΛ஗Βͤͨ

  16. ʮ҆શʯͳιʔείʔυ͸ɺ όάΛຊ൪ϦϦʔεલʹ 
 ؾ͔ͮͤΔ΂͖ɻ

  17. ͜ͷίʔυͬͯɺ҆શʁ func doSomething() { let image = UIImage(named: "ImageName") ??

    UIImage() // do something with image }
  18. ͜ͷίʔυͷํ͕ɺ҆શ func doSomething() { let image = UIImage(named: "ImageName")! //

    do something with image } όϯυϧʹೖͬͯͳ͍ͱ͍͚ͳ͍͸ͣͷ 
 ը૾Ϧιʔε͔ͩΒɺ ສ͕Ұೖͬͯͳ͔ͬͨΒͦΕ͸όάͳͷͰ ಈ࡞֬ೝͰΫϥογϡͯ͘͠Εͨํ͕ 
 ؾ෇͖΍͍͢
  19. ͜ͷίʔυͬͯɺ҆શʁ func doSomething() { guard let appDelegate = UIApplication .shared.delegate

    as? AppDelegate else { return } // do something with appDelegate }
  20. ͜ͷίʔυͷํ͕ɺ҆શ func doSomething() { let appDelegate = UIApplication.shared.delegate as! AppDelegate

    // do something with appDelegate } ϓϩδΣΫτͷ࡞Γ͕ 
 ͜ͷΑ͏ʹͳͬͯΔ͸ͣͳͷͰɺ ສ͕Ұ͜ΕͰμ΢ϯΩϟετͰ͖ͳ͔ͬͨΒ ઈରϓϩάϥϜ͕Կ͔ؒҧͬͯΔ͸͔ͣͩΒɺ ಈ࡞֬ೝͰΫϥογϡͯ͘͠Εͨํ͕ ؾ෇͖΍͍͢
  21. !Λ࢖͏΂͖ॴͰ࢖ͬͨํ͕ɺ όάͷૣظൃݟʹͭͳ͕Γɺ Ή͠Ζ҆қͳѲΓ௵͠ΑΓ҆શɻ

  22. ౰વɺཚ༻ͯ͠͸͍͚ͳ͍ɻ

  23. ͜ͷίʔυͬͯɺ҆શʁ func doSomething(with data: Data) { do { let decoder

    = JSONDecoder() let response = try decoder.decode(Response.self, from: data) // do something with response } catch { debugPrint(error) return } }
  24. ͜ͷίʔυͷํ͕ɺ҆શ func doSomething(with data: Data) { do { let decoder

    = JSONDecoder() let response = try decoder.decode(Response.self, from: data) // do something with response } catch { assertionFailure("\(error)") return } } ԾʹαʔόͷσʔλͱҰக͠ͳ͔ͬͨΒ BTTFSUJPO'BJMVSFʹΑͬͯ σόοάϏϧυ͚ͩΫϥογϡͯ͠ 
 όάΛૣظൃݟ͠ɺ ຊ൪ͷϦϦʔεϏϧυʹ͸Өڹͳ͠ɻ ʢ΋ͪΖΜ͜͜ͰߋʹΤϥʔΞϥʔτΛ 
 Ϣʔβʹදࣔͤͨ͞Βͳ͓ྑ͠ʣ
  25. ͜ͷίʔυ͸ɺ͕͢͞ʹ҆શͰ͸ͳ͍ func doSomething(with data: Data) { let decoder = JSONDecoder()

    let response = try! decoder.decode(Response.self, from: data) // do something with response } ສ͕Ұαʔόଆ͕όάΛى͜Γ 
 ຊདྷͷ࢓༷ͱ͸ҧ͏ܕͷϨεϙϯε͕ 
 ؼ͖ͬͯͨΒ 
 ΞϓϦ͕ଈΫϥογϡ͢ΔͷͰ 
 ྲྀੴʹྑ͘ͳ͍
  26. 5*14ᶃ w ͦΕͰ΋΍͸ΓͲ͏ͯ͠΋ຊ൪ϦϦʔεͰΫϥογϡ ͯ͠ཉ͘͠ͳ͍ɻԿ͔ํ๏͋Δʁ w assertionFailureΛ࢖͑͹ɺσόοάϏϧυͩ ͚ΫϥογϡͰ͖ΔͷͰɺͥͻ׆༻͠·͠ΐ͏ɻ w ྫɿ let

    image = UIImage(named: "ImageName") ?? { assertionFailure("ImageName not found") return UIImage() }()
  27. 5*14ᶄ w ଞʹ΋Ͳ͔͜ੵۃతʹ!Λ࢖ͬͨํ͕৔໘͋Δʁ w ςετίʔυ΋Ή͠ΖੵۃతʹόάΛؾ෇͖͍ͨ͠ ຊ൪ΞϓϦʹҰ੾Өڹ͠ͳ͍ͷͰɺੵۃతʹ!Λ࢖ ͑Δ͜ͱ͕ଟ͍Ͱ͢ɻ w ͨͩ͠Ϋϥογϡ͢Δͱޙଓͷίʔυ΋ಈ͔ͳ͘ ͳΔͷͰɺ৔ॴʹΑͬͯ͸XCTFailΛ࢖ͬͨํ

    ͕͍͍͜ͱ΋͋Γ·͢ w 1MBZHSPVOEͱ͔ͷຊ൪ΞϓϦؔ܎ͳࣗ͘෼Ͱखܰ ͳಈ࡞֬ೝ͍ͨ࣌͠΋΋ͪΖΜੵۃతʹ࢖ͬͯΑ͠
  28. !Λա౓ʹڪΕͣʹ ͏·͘׆༻ͯ͠ όάΛૣظʹ௵͠·͠ΐ͏