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

͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ