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

これでよくわかる!Swiftのオプショナル

53e2d354b3299d64a54af680865516d5?s=47 Sato Takeshi
February 28, 2016

 これでよくわかる!Swiftのオプショナル

iOS開発会議 in beez渋谷 47回目に発表した資料です。

53e2d354b3299d64a54af680865516d5?s=128

Sato Takeshi

February 28, 2016
Tweet

More Decks by Sato Takeshi

Other Decks in Technology

Transcript

  1. ʲே׆ʳ J04։ൃձٞ JOCFF[ौ୩ ճ໨ ೥݄೔ ࠤ౻߶࢜

  2. ࣗݾ঺հ ʲ໊લʳ ࠤ౻߶࢜ ʲݞॻʳ ίϯηϓτͮ͘Γ͔Β૬ஊͰ͖ΔΤϯδχΞɻ ʲ͓࢓ࣄʳ ࡢ೥݄ʹಠཱɻ લ͸डୗ*5اۀͰΠϯϑϥͷอकӡ༻ۀ຿ ࠓ͸༗ݶձࣾΧΠΧΠͰJ04ΞϓϦ࡞੒ ʲͰ͖Δ͜ͱʳ

    SVCZ J04 BQBUDI MJOVY αʔόʔपΓʣ 5XJMJP ʲϦϦʔεΞϓϦʳ :VNF)PTIJ
  3. 4XJGUΛ։ൃͯͯ͠ 9DPEF͕΍ Λ ͔ͬͯʹೖΕͯ͘ΕΔ

  4. 9DPEFͷ͞ΕΔ͕··ʹ ௚͢ͷ͕ؾ࣋ͪѱ͍

  5. 4XJGUͷΦϓγϣφϧܕ Λ੔ཧͯ͠ ͳΜͱͳ͘ ΍Λ͚ͭΔͷΛ ΍Ί͍ͨ ɾТɾʆ

  6. 0CKFDU$Ͱͷ։ൃ ࣮ߦதͰͳ͍ͱ෼͔Βͳ͍όάΛ͏͔ͬΓ࡞ͬͪΌ͏ - (IBAction)crash:(UIButton *)sender { NSArray *array = [NSArray

    array]; //Ϋϥογϡͤ͞Δ NSLog(@"%@", array[0]); }
  7. ΞϓϦམͪͨʔ

  8. ͦͷΤϥʔ 4XJGUͷΦϓγϣφϧܕͳΒ ࣄલʹݟ͚ͭΒΕΔ͔΋

  9. 4XJGUͷΦϓγϣφϧͰΨʔυ ੩తղੳͰΤϥʔʹͳΓͦ͏ͳͱ͜Ζ͕൑ผͰ͖Δ ˠજࡏతͳόάΛऔΓআ͖΍͍͢ʂ @IBAction func crash(sender: UIButton) { var array

    : Array<String>? print(array?[0]) }
  10. ͦΜͳ4XJGUͷΦϓγϣφϧܕ ͱ΋ͬͱ஥ྑ͘ͳΓ͍ͨ ɾ㱼ɾ

  11. 4XJGUͷΦϓγϣφϧܕ ·ͱΊ

  12. 0QUJPOBMܕͱ͸ʁ 0QUJPOBMܕOJMͷ୅ೖΛڐ͢ ඇPQUJPOBMܕOJMͷ୅ೖΛڐ͞ͳ͍ 0CKFDUJWF$ʹ͸ΦϒδΣΫτʹ͔͠OJM͸ͳ͔ͬͨɻ ߏ଄ମɺ$ݴޠͷܕɺྻڍܕʹOJM͕ͳ͍ʣ TXJGU͸ͲΜͳܕ΋Φϓγϣφϧ࢖͑Δ

  13. 0QUJPOBMܕએݴ var a: Int? var b: Optional<Int> // Int?ɹͱಉ͡ҙຯ enum

    Optional<T> : Reflectable, NilLiteralConvertible { case None // nil ʹ૬౰͢Δ case Some(T) // T ܕʢૉͷܕʣͷ஋͕ೖΔʢInt ܕ΍ String ܕͷ஋ͳͲʣ } 0QUJPOBM5͸FOVNܕͰఆٛ
  14. 0QUJPOBMܕએݴ͢ΔͱOJM͕ೖͬͯ΋͍͍ var a: Int? a = nil //->nil͕ೖͬͯ΋େৎ෉ var b:

    Int // ඇ optional ܕ b = nil // -> Nil cannot be assigned to type 'Int' ඇPQUJPOBMܕʹOJMΛ୅ೖ͠Α͏ͱ͢ΔͱɺίϯύΠϥΤϥʔ͕ൃੜ͢Δɻ 0QUJPOBMܕͷॳظ஋͸OJM
  15. 0QUJPOBMܕͱඇ0QUJPOBMܕ͸ผͷܕ var unoptionalInt: Int = 1 print(unoptionalInt + 2) //

    -> 3 var optionalInt : Int? = 1 print(optionalInt + 2) //Value of optional type 'Int?' not unwrapped; 0QUJPOBMܕΛ࢖͏ʹ͸Ξϯϥοϓ͕ඞཁ
  16. Ξϯϥοϓͷํ๏ w 'PSDFEVOXSBQQJOH w 0QUJPOBMDIBJOJOH w 0QUJPOBMCJOEJOH w ൺֱԋࢉࢠ w

    *NQMJDJUMZ6OXSBQQFE0QUJPOBMܕͰએݴ͢Δ
  17. 'PSDFEVOXSBQQJOH var optionalInt : Int? = 1 print(optionalInt! + 2)

    0QUJPOBMܕͷม਺ͷޙΖʹʮʯΛ͚ͭΔ OJMͩͬͨΒϥϯλΠϜΤϥʔ
  18. 0QUJPOBMDIBJOJOH class Room { var adress : String = "ौ୩۠"

    } class Person { var room : Room? } var aperson = Person() aperson.room?.adress = "໨ࠇ۠" aperson.room = Room() print(aperson.room?.adress) ʮ ʯΛ࢖ͬͯΞϯϥοϓ͢Δ͜ͱ ΞϯϥοϓΛͨ͠ޙʹɺ࠷ऴతͳ໭Γ஋ΛPQUJPOBMܕʹϥοϓͯ͠ ͔Βฦ͢ɻ
  19. 0QUJPOBM#JOEJOH if let adress = aperson.room?.adress{ print(adress) }else{ print("ॅॴ͕ొ࿥͞Ε͍ͯ·ͤΜ") }

    while let adress = aperson.room?.adress{ print(adress) break } JG΍XIJMFจͷ৚݅ࣜͰએݴ͞ΕɺPQUJPOBMܕͷม਺Λ୅ೖ͞Εͨ ม਺͸ɺඇPQUJPOBMܕʹͳΔɻ ͜ΕΛʮPQUJPOBMCJOEJOHʯͱݴ͏
  20. ൺֱԋࢉࢠ var wrappedInt: Int? = 1 // Optional ܕ print(wrappedInt

    == 1) // -> true print(wrappedInt >= 1) // -> true print(wrappedInt > 1) // -> false print(wrappedInt <= 1) // -> true print(wrappedInt < 1) // -> false print(wrappedInt != 1) // -> false wrappedInt = nil print(wrappedInt == 1) // -> false ൺֱԋࢉࢠΛ࢖͏ͱɺPQUJPOBMܕͷม਺͕ࣗಈతʹΞϯϥοϓ͞Ε Δɻ OJM͕ೖͬͯ΋Τϥʔʹ͸ͳΒͳ͍
  21. *NQMJDJUMZ6OXSBQQFE0QUJPOBMܕͰએݴ /**implicitly unwrapped optional*/ var assumedString: String! = "An implicitly

    unwrapped optional string." let implicitString: String = assumedString // no need for an exclamation mark” print(implicitString) /*ΦϓγϣφϧόΠϯσΟϯά΋Ͱ͖Δ*/ if let definiteString = assumedString { print(definiteString) } ࣗಈతʹΞϯϥοϓ͞ΕΔPQUJPOBMܕ ʮʯͰએݴ͢Δ
  22. *NQMJDJUMZ6OXSBQQFE0QUJPOBMܕͰએݴ var implicityInt: Int! var implicityInt2: ImplicitlyUnwrappedOptional<Int> // Int! ͱಉ͡ҙຯ

    ʮʯ͸*NQMJDJUMZ6OXSBQQFE0QUJPOBM5ͷγϯλοΫεγϡΨʔɻ *NQMJDJUMZ6OXSBQQFE0QUJPOBM5͸FOVNܕͰఆٛ͞Ε͍ͯΔ enum ImplicitlyUnwrappedOptional<T> : Reflectable, NilLiteralConvertible { case None // nil ʹ૬౰͢Δ case Some(T) // T ܕʢૉͷܕʣͷ஋͕ೖΔʢInt ܕ΍ String ܕͷ ஋ͳͲʣ }
  23. ·ͱΊ એݴ࣌ ࣮ߦ࣌ 0QUJPOBMܕ 0QUJPOBMDIBJOJOH  *NQMJDJUMZ 6OXSBQQFE 0QUJPOBMܕ 'PSDFEVOXSBQQJOH

  24. αϯϓϧίʔυ IUUQTHJTUHJUIVCDPN4BUP5BLFTIJ9FFCDGCFBDDF

  25. ࢀߟ63- 5IF4XJGU1SPHSBNNJOH-BOHVBHF 4XJGU  IUUQTJUVOFTBQQMFDPNKQCPPLTXJGUQSPHSBNNJOH MBOHVBHFJE NU OVMMࢀরͷߟҊ͸ԯυϧ୯Ґͷաͪʁ IUUQEFWFMPQFSTTSBEKQTUPSZOVMM &'&"&"&

    &&"&"'&& &"#&%&#%%&"& &&&"&'#$'
  26. ࢀߟ63- <4XJGU>4XJGUͷPQUJPOBMWBMVFͷศར͞ʮ ʯͱʮʯͰΑΓݎ࿚ͳ ίʔυ΁ IUUQRJJUBDPNOPSJJUFNTBGBDDGGCED <4XJGU>0QUJPOBMܕʹ͍ͭͯͷ·ͱΊ7FS IUUQRJJUBDPNDPUSQFQFJUFNTDDBBGG 4XJGUͷΦϓγϣφϧνΣʔϯʢPQUJPOBMDIBJOJOHʣ IUUQCMPHLJUPLPDPNFOUSZ ΦϓγϣφϧܕɻʙͳΜͱͳ͘෇͚Δ

    ๾໓ IUUQXXXTMJEFTIBSFOFUIBTFHBXBUPNPLJTT