if 式と switch 式による SwiftUI のプレビューエラー対策
by
ojun
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
potatotips #85 iOS/Android։ൃTipsڞ༗ձ 2023/11/14 @Voicy if ࣜͱ switch ࣜʹΑΔ SwiftUI ͷϓϨϏϡʔΤϥʔରࡦ ojun - DeNA Co., Ltd.
Slide 2
Slide 2 text
ࣗݾհʢojunʣ • name: ojun • 2023৽ଔೖࣾ • iOSΤϯδχΞ @ojun_9
Slide 3
Slide 3 text
Xcode 15 ରԠਐΜͰ͍·͔͢ʁ
Slide 4
Slide 4 text
Xcode 15.0 Asset Catalogs / String Catalogs PreviewϚΫϩ Swift 5.9 ϒοΫϚʔΫػೳ ɾෳͷݴޠػೳͷՃ ɾσόοά࣌ʹมͷ༰Λग़ྗ͢ΔͨΊʹར༻͢ΔpίϚϯυͱpoίϚϯυ ʹΑΔग़ྗ͕࣌ؒେ෯ʹॖ ͦͷଞ͍Ζ͍Ζ
Slide 5
Slide 5 text
Xcode 15.0 Asset Catalogs / String Catalogs PreviewϚΫϩ Swift 5.9 ϒοΫϚʔΫػೳ ɾෳͷݴޠػೳͷՃ ͦͷଞ͍Ζ͍Ζ ɾσόοά࣌ʹมͷ༰Λग़ྗ͢ΔͨΊʹར༻ՄೳͳpίϚϯυͱpoίϚϯυ ʹΑΔग़ྗ͕࣌ؒେ෯ʹॖ
Slide 6
Slide 6 text
Swift 5.9: if ͱ switch ͷࣜදݱ
Slide 7
Slide 7 text
Swift 5.9: ifͱswitchͷࣜදݱ Swift 5.9͔ΒswitchͱifΛࣜͱͯ͠දݱͰ͖ΔΑ͏ʹͳͬͨ
Slide 8
Slide 8 text
Swift 5.9: ifͱswitchͷࣜදݱ Swift 5.9͔ΒswitchͱifΛࣜͱͯ͠දݱͰ͖ΔΑ͏ʹͳͬͨ
Slide 9
Slide 9 text
Swift 5.9: ifͱswitchͷࣜදݱ Swift 5.9͔ΒswitchͱifΛࣜͱͯ͠දݱͰ͖ΔΑ͏ʹͳͬͨ
Slide 10
Slide 10 text
Swift 5.9: ifͱswitchͷࣜදݱ Swift 5.9͔ΒswitchͱifΛࣜͱͯ͠දݱͰ͖ΔΑ͏ʹͳͬͨ
Slide 11
Slide 11 text
Swift 5.9: ifͱswitchͷࣜදݱ Swift 5.9͔ΒswitchͱifΛࣜͱͯ͠දݱͰ͖ΔΑ͏ʹͳͬͨ
Slide 12
Slide 12 text
Preview࣌ʹར༻͢Δ
Slide 13
Slide 13 text
Preview࣌ʹར༻͢Δ isSwiftͷBoolʹج͍ͮͯTextΛฦ͢
Slide 14
Slide 14 text
Preview࣌ʹར༻͢Δ isSwiftͷBoolʹج͍ͮͯTextΛฦ͢ ௨ৗͷϏϧυ 🎉 ͳ͘ޭ͢Δ
Slide 15
Slide 15 text
Preview࣌ʹར༻͢Δ isSwiftͷBoolʹج͍ͮͯTextΛฦ͢ ௨ৗͷϏϧυ 🎉 ͳ͘ޭ͢Δ Preview࣌ͷϏϧυ 😭 ίϯύΠϧΤϥʔ͕ൃੜ͢Δ
Slide 16
Slide 16 text
Preview࣌ʹར༻͢Δ isSwiftͷBoolʹج͍ͮͯTextΛฦ͢ ௨ৗͷϏϧυ 🎉 ͳ͘ޭ͢Δ Preview࣌ͷϏϧυ 😭 ίϯύΠϧΤϥʔ͕ൃੜ͢Δ Preview ProviderΛར༻ͯ͠ಉ༷
Slide 17
Slide 17 text
Preview࣌ʹར༻͢Δ 'if' may only be used as expression in return, throw, or as the source of an assignment
Slide 18
Slide 18 text
ղܾํ๏
Slide 19
Slide 19 text
ղܾํ๏ 1. @ViewBuilderΩʔϫʔυΛ༩͢Δ 2. Opaque typeͰͳ͍໌֬ͳܕΛฦ͢ 3. Opaque Type Λ Existential Type ʹஔ͖͑Δ
Slide 20
Slide 20 text
@ViewBuilderΩʔϫʔυΛ༩͢Δ @ViewBuilderΩʔϫʔυΛ༩͢Δ ͜ͱͰPreview࣌ͷΤϥʔΛճආͰ͖Δ ௨ৗͷϏϧυ 🎉 ͳ͘ޭ͢Δ Preview࣌ͷϏϧυ 🎉 ͳ͘ޭ͢Δ
Slide 21
Slide 21 text
໌֬ͳܕΛฦ͢ ௨ৗͷϏϧυ 🎉 ͳ͘ޭ͢Δ Preview࣌ͷϏϧυ 🎉 ͳ͘ޭ͢Δ `Text`ͷΑ͏ʹܕΛ໌֬ʹ͢Δ ͜ͱͰPreview࣌ͷΤϥʔΛճආͰ͖Δ
Slide 22
Slide 22 text
Opaque Type Λ Existential Type ʹஔ͖͑Δ ΓΛany Viewʹ͢Δํ๏ ͨͩ͠ɺݺͼग़͠ݩͰܕΩϟετΛ ߦ͏ඞཁ͕͋Δ ௨ৗͷϏϧυ 🎉 ͳ͘ޭ͢Δ Preview࣌ͷϏϧυ 🎉 ͳ͘ޭ͢Δ
Slide 23
Slide 23 text
ͷݪҼΛߟ͢Δ ʢࠜຊݪҼಥ͖ࢭΊΒΕ·ͤΜͰͨ͠😢ʣ
Slide 24
Slide 24 text
ͷݪҼΛߟ͢Δ ContentView.1.preview-thunk.swift ͱ͍͏ϑΝΠϧ͕ҎԼʹଘࡏ͢Δ ~/Library/Developer/Xcode/DerivedData/MakeViewFromSwitch- fghepcludbkyehbmhgmucvzdrxsk/Build/Intermediates.noindex/Previews/ MakeViewFromSwitch/Intermediates.noindex/MakeViewFromSwitch.build/ Debug-iphonesimulator/MakeViewFromSwitch.build/Objects-normal/arm64
Slide 25
Slide 25 text
ContentViewͷPreviewϏϧυ͕Δͱ͜ͷϑΝΠϧ͕ߋ৽͞ΕΔ
Slide 26
Slide 26 text
ྫ͑ɺany ErrorΛฦ͢Α͏ͳϝιουΛ࣮͢Δͱ
Slide 27
Slide 27 text
ContentView.1.preview-thunk.swift ߋ৽͞ΕΔ
Slide 28
Slide 28 text
ΓΛOpaque Typeʹ͢Δͱ ͨͩ͠
Slide 29
Slide 29 text
ContentView.1.preview-thunk.swift ͔ΒgetErrorͷॲཧ͕ফ͑Δ
Slide 30
Slide 30 text
ͷݪҼΛߟ͢Δ ίϯύΠϧΤϥʔʹͳ࣮ͬͨ Opaque Typeͩͬͨ తͳݪҼ͔Θ͔Βͳ͍͕͜Ε͕ ʹͳ͍ͬͯΔՄೳੑ͕͋Δ
Slide 31
Slide 31 text
·ͱΊ Swift 5.9͔Β if ͱ switch Λࣜͱͯ͠ѻ͑ΔΑ͏ʹͳͬͨ if / switch ࣜΛ༻͍ͯSwiftUIͷViewΛʢopaque typeͱͯ͠ʣฦ͢ͱ Preview࣌ͷϏϧυͰͷΈίϯύΠϧΤϥʔ͕ൃੜ͢Δ ҎԼͷ͍ͣΕ͔ͷํ๏ͰͷରԠΛ͢Δ͜ͱ͕Ͱ͖Δ 1. @ViewBuilderΩʔϫʔυΛ༩͢Δ 2. Opaque typeͰͳ͍໌֬ͳܕΛฦ͢ʢText ͳͲʣ 3. Opaque Type Λ Existential Type ʹஔ͖͑Δ
Slide 32
Slide 32 text
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ