Save 37% off PRO during our Black Friday Sale! »

Demystify SwiftUI 要約 / Summary of Demystify SwiftUI

Demystify SwiftUI 要約 / Summary of Demystify SwiftUI

Af64bc38c0ffcfcabdf430759ee491ce?s=128

Elvis Shi

June 28, 2021
Tweet

Transcript

  1. %FNZTUJGZ4XJGU6*ཁ໿ f o r  : 6 . & .

    *  T X J G U      G F BU   ) " , "5"  T X J G U  
 ʙ 8 8 % $  3F DBQ ʙ
  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. ˞ೣͰྫΛ্͍͛ͯ·͕͢ɺൃදऀ͸Πψ೿Ͱ͢ɻ

  4. ࣌ؒ ҧ͏࣌ؒͷ ಉ͡ೣʁ

  5. ࣌ؒ ͦ΋ͦ΋ 
 ҧ͏ೣʁ

  6. ࣸਅ͚ͩͰ͸൑அ͕Ͱ͖ͳ͍ w ҧ͏ೣͰ΋ͱͯ΋ࣅͯΔ͔΋͠Εͳ͍ w ಉ͡ೣͰ΋࣌ظʹΑͬͯݟͨ໨͕ҧ͏͔΋͠Εͳ͍ w ൑அ͢ΔͨΊʹ͸௥Ճͷ৘ใ͕ඞཁ w ໊લ w

    ೒࠼ w FUD *EFOUJUZ
  7. 4XJGU6*Λཧղ͢ΔͨΊͷେϙΠϯτ w *EFOUJUZ w -JGFUJNF w %FQFOEFODZ 4XJGU6*͕͋ͳͨͷࢥ͏௨Γͷಈ࡞Λ͍ͯ͠ͳ͔ͬͨΒɺ 
 େ఍͸͜ΕΒͷѻ͍͕ؒҧ͍ͬͯΔ͔Βɻ

  8. *EFOUJUZ w େϙΠϯτͷதͰ΋Ұ൪ॏཁ w -JGFUJNF΋%FQFOEFODZ΋ɺ*EFOUJUZ͕͋Δ͔Β ͦ͜੒Γཱͭ w 4XJGU6*ʹ͸छྨͷ*EFOUJUZ͕͋Δ w &YQMJDJU*EFOUJUZʢ໌ࣔత*%ʣ

    w 4USVDUVSBM*EFOUJUZʢߏ଄త*%ʣ
  9. &YQMJDJU*EFOUJUZ w ಈతʹมΘΔϏϡʔʹɺ ΤϯδχΞ͕͚ͭΔ*% w ର৅͕Identifiable ʹద߹͍ͯ͠ͳ͍৔߹͸ *%΁ͷKeyPathΛࢦఆ ͢Δඞཁ͕͋Δ ForEach(cats,

    id: \.name) { cat in Text("\(cat.age)ࡀ") } Explicit Identity
  10. &YQMJDJU*EFOUJUZʹٻΊΒΕΔੑ࣭ w ϢχʔΫੑ w ଞͷର৅ͱඃͬͯ͸͍ ͚ͳ͍ w ෆมੑ w ͍ͭɺԿճݺͼग़ͯ͠

    ΋มΘͬͯ͸͍͚ͳ͍ ForEach(cats, id: \.name) { cat in Text("\(cat.age)ࡀ") } Explicit Identity
  11. 4USVDUVSBM*EFOUJUZ w ߏ଄ʹΑͬͯࣗಈతʹ෇ ͚ΒΕΔ*% w JGจ΍TXJUDIจͷ෼ذ ͕͋ͬͨΒҧ͏*%͕෇ ͚ΒΕΔ Structural Identity

    if flag { Text("A") } else { Text("B") }
  12. 4USVDUVSBM*EFOUJUZͱͷ෇͖߹͍ํ w ಉ͡ϏϡʔͳΒɺ෼ذΛ ͯ͠͸ߦ͚ͳ͍ w ෼ذΛͤͣʹ͏·͘ *OFSU.PEJ fi FSͳͲΛ ׆༻

    Structural Identity if flag { Text("A") } else { Text("B") } ద༻ͯ͠΋ϏϡʔͷඳըʹӨڹ͠ͳ͍.PEJ fi FSɻ  PQBDJUZ    QBEEJOH    FUD
  13. ͳͥ*EFOUJUZ͕ॏཁ͔ w "OJNBUJPO͢Δͱ͖ͳͲͷ൑அࠜڌ w Ҡಈ͢΂͖͔ΫϩεϑΣʔυ͢΂͖͔ w Ϗϡʔͷ-JGFUJNFΛܾΊΔࠜڌ w ඞཁͳ͘ͳͬͨΒϝϞϦ͔Βফ͢

  14. -JGFUJNF w 4XJGU6*ͷϏϡʔλΠϓ͸஋ܕ w ϏϡʔλΠϓ͸ͨͩͷϏϡʔඳըϨγϐ w ϏϡʔλΠϓͷΠϯελϯεࣗମ͸ඇৗʹ୹໋ w ͔͠͠Ϗϡʔͷੜଘظؒ͸΋ͬͱ௕͍ w

    Ϗϡʔ͸ඞཁ͕͋ͬͨΒੜ੒͞Εɺඞཁͳ͘ͳͬͨΒফ͞ΕΔ w ੜ੒͞ΕͨΒonAppearɺফ͞ΕͨΒonDisappear͕ݺ ͹ΕΔ w @State΍@StateObject΋Ϗϡʔ͕ඞཁͳ͘ͳͬͨΒফ ͞ΕΔ
  15. -JGFUJNF fl BHUSVF  UBQ ࣌ؒ fl BHGBMTF struct Demo:

    View { var flag: Bool var body: some View { if flag { Counter() .background(Color.blue) } else { Counter() .background(Color.red) } } } struct Counter: View { @State private var count = 0 var body: some View { Text("\(count)") .onTapGesture { count += 1 } } }    fl BHUSVF   ফ͞ΕΔ
  16. -JGFUJNF fl BHUSVF  UBQ ࣌ؒ fl BHGBMTF struct Demo:

    View { var flag: Bool var body: some View { Counter() .background( flag ? Color.blue : Color.red ) } } struct Counter: View { @State private var count = 0 var body: some View { Text("\(count)") .onTapGesture { count += 1 } } }   fl BHUSVF  ফ͞Εͳ͍ Dependency
  17. %FQFOEFODZ w %FQFOEFODZ͸ͨͩͷϏϡʔͷೖྗ w @State΍@StateObject͚ͩͰͳ͘ɺ @Binding΍ͨͩͷvar΋શͯ%FQFOEFODZ w bodyҎ֎શͯ%FQFOEFODZͱߟ͑ͯ໰୊ͳ͍ w %FQFOEFODZ͕มΘͬͨΒɺͦΕʹඥͮ͘Ϗϡʔ͕

    ࠶ඳը͞ΕΔ w CPEZ͕ݺ͹Ε࣮ͯߦ͞ΕΔ
  18. %FQFOEFODZ 7JFX 7JFX 7JFX 7JFX %FQFOEFODZ %FQFOEFODZ %FQFOEFODZ

  19. ·ͱΊ w *EFOUJUZΛ੍͢Δ΋ͷ͸4XJGU6*Λ੍͢Δ w -JGFUJNFΛ஫ҙ͠ͳ͍ͱඞཁͳঢ়ଶ͕ফ͑ͯ͠·͏ w %FQFOEFODZͰ࠷ඳը͢ΔϏϡʔΛநग़͍ͯ͠Δ

  20. ࢀߟ IUUQTEFWFMPQFSBQQMFDPNWJEFPTQMBZXXED