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

if 式と switch 式による SwiftUI のプレビューエラー対策

ojun
November 14, 2023

if 式と switch 式による SwiftUI のプレビューエラー対策

ojun

November 14, 2023
Tweet

More Decks by ojun

Other Decks in Programming

Transcript

  1. potatotips #85 iOS/Android։ൃTipsڞ༗ձ
    2023/11/14 @Voicy
    if ࣜͱ switch ࣜʹΑΔ
    SwiftUI ͷϓϨϏϡʔΤϥʔରࡦ
    ojun - DeNA Co., Ltd.

    View full-size slide

  2. ࣗݾ঺հʢojunʣ
    • name: ojun
    • 2023೥৽ଔೖࣾ
    • iOSΤϯδχΞ
    @ojun_9

    View full-size slide

  3. Xcode 15 ରԠਐΜͰ͍·͔͢ʁ

    View full-size slide

  4. Xcode 15.0
    Asset Catalogs / String Catalogs
    PreviewϚΫϩ
    Swift 5.9
    ϒοΫϚʔΫػೳ
    ɾෳ਺ͷݴޠػೳͷ௥Ճ
    ɾσόοά࣌ʹม਺ͷ಺༰Λग़ྗ͢ΔͨΊʹར༻͢ΔpίϚϯυͱpoίϚϯυ
    ʹΑΔग़ྗ͕࣌ؒେ෯ʹ୹ॖ
    ͦͷଞ͍Ζ͍Ζ

    View full-size slide

  5. Xcode 15.0
    Asset Catalogs / String Catalogs
    PreviewϚΫϩ
    Swift 5.9
    ϒοΫϚʔΫػೳ
    ɾෳ਺ͷݴޠػೳͷ௥Ճ
    ͦͷଞ͍Ζ͍Ζ
    ɾσόοά࣌ʹม਺ͷ಺༰Λग़ྗ͢ΔͨΊʹར༻ՄೳͳpίϚϯυͱpoίϚϯυ
    ʹΑΔग़ྗ͕࣌ؒେ෯ʹ୹ॖ

    View full-size slide

  6. Swift 5.9: if ͱ switch ͷࣜදݱ

    View full-size slide

  7. Swift 5.9: ifͱswitchͷࣜදݱ
    Swift 5.9͔ΒswitchͱifΛࣜͱͯ͠දݱͰ͖ΔΑ͏ʹͳͬͨ

    View full-size slide

  8. Swift 5.9: ifͱswitchͷࣜදݱ
    Swift 5.9͔ΒswitchͱifΛࣜͱͯ͠දݱͰ͖ΔΑ͏ʹͳͬͨ

    View full-size slide

  9. Swift 5.9: ifͱswitchͷࣜදݱ
    Swift 5.9͔ΒswitchͱifΛࣜͱͯ͠දݱͰ͖ΔΑ͏ʹͳͬͨ

    View full-size slide

  10. Swift 5.9: ifͱswitchͷࣜදݱ
    Swift 5.9͔ΒswitchͱifΛࣜͱͯ͠දݱͰ͖ΔΑ͏ʹͳͬͨ

    View full-size slide

  11. Swift 5.9: ifͱswitchͷࣜදݱ
    Swift 5.9͔ΒswitchͱifΛࣜͱͯ͠දݱͰ͖ΔΑ͏ʹͳͬͨ

    View full-size slide

  12. Preview࣌ʹར༻͢Δ

    View full-size slide

  13. Preview࣌ʹར༻͢Δ
    isSwiftͷBool஋ʹج͍ͮͯTextΛฦ͢

    View full-size slide

  14. Preview࣌ʹར༻͢Δ
    isSwiftͷBool஋ʹج͍ͮͯTextΛฦ͢
    ௨ৗͷϏϧυ
    🎉 ໰୊ͳ͘੒ޭ͢Δ

    View full-size slide

  15. Preview࣌ʹར༻͢Δ
    isSwiftͷBool஋ʹج͍ͮͯTextΛฦ͢
    ௨ৗͷϏϧυ
    🎉 ໰୊ͳ͘੒ޭ͢Δ
    Preview࣌ͷϏϧυ
    😭 ίϯύΠϧΤϥʔ͕ൃੜ͢Δ

    View full-size slide

  16. Preview࣌ʹར༻͢Δ
    isSwiftͷBool஋ʹج͍ͮͯTextΛฦ͢
    ௨ৗͷϏϧυ
    🎉 ໰୊ͳ͘੒ޭ͢Δ
    Preview࣌ͷϏϧυ
    😭 ίϯύΠϧΤϥʔ͕ൃੜ͢Δ
    Preview ProviderΛར༻ͯ͠΋ಉ༷

    View full-size slide

  17. Preview࣌ʹར༻͢Δ
    'if' may only be used as expression
    in return, throw, or as the source
    of an assignment

    View full-size slide

  18. ղܾํ๏
    1. @ViewBuilderΩʔϫʔυΛ෇༩͢Δ
    2. Opaque typeͰ͸ͳ͍໌֬ͳܕΛฦ͢
    3. Opaque Type Λ Existential Type ʹஔ͖׵͑Δ

    View full-size slide

  19. @ViewBuilderΩʔϫʔυΛ෇༩͢Δ
    @ViewBuilderΩʔϫʔυΛ෇༩͢Δ
    ͜ͱͰPreview࣌ͷΤϥʔΛճආͰ͖Δ
    ௨ৗͷϏϧυ
    🎉 ໰୊ͳ͘੒ޭ͢Δ
    Preview࣌ͷϏϧυ
    🎉 ໰୊ͳ͘੒ޭ͢Δ

    View full-size slide

  20. ໌֬ͳܕΛฦ͢
    ௨ৗͷϏϧυ
    🎉 ໰୊ͳ͘੒ޭ͢Δ
    Preview࣌ͷϏϧυ
    🎉 ໰୊ͳ͘੒ޭ͢Δ
    `Text`ͷΑ͏ʹܕΛ໌֬ʹ͢Δ
    ͜ͱͰPreview࣌ͷΤϥʔΛճආͰ͖Δ

    View full-size slide

  21. Opaque Type Λ Existential Type ʹஔ͖׵͑Δ
    ໭Γ஋Λany Viewʹ͢Δํ๏
    ͨͩ͠ɺݺͼग़͠ݩͰܕΩϟετΛ
    ߦ͏ඞཁ͕͋Δ
    ௨ৗͷϏϧυ
    🎉 ໰୊ͳ͘੒ޭ͢Δ
    Preview࣌ͷϏϧυ
    🎉 ໰୊ͳ͘੒ޭ͢Δ

    View full-size slide

  22. ໰୊ͷݪҼΛߟ࡯͢Δ
    ʢࠜຊݪҼ͸ಥ͖ࢭΊΒΕ·ͤΜͰͨ͠😢ʣ

    View full-size slide

  23. ໰୊ͷݪҼΛߟ࡯͢Δ
    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

    View full-size slide

  24. ContentViewͷPreviewϏϧυ͕૸Δͱ͜ͷϑΝΠϧ͕ߋ৽͞ΕΔ

    View full-size slide

  25. ྫ͑͹ɺany ErrorΛฦ͢Α͏ͳϝιουΛ࣮૷͢Δͱ

    View full-size slide

  26. ContentView.1.preview-thunk.swift ΋ߋ৽͞ΕΔ

    View full-size slide

  27. ໭Γ஋ΛOpaque Typeʹ͢Δͱ
    ͨͩ͠

    View full-size slide

  28. ContentView.1.preview-thunk.swift ͔ΒgetErrorͷॲཧ͕ফ͑Δ

    View full-size slide

  29. ໰୊ͷݪҼΛߟ࡯͢Δ
    ίϯύΠϧΤϥʔʹͳ࣮ͬͨ૷͸
    Opaque Typeͩͬͨ
    ௚઀తͳݪҼ͔͸Θ͔Βͳ͍͕͜Ε͕
    ໰୊ʹͳ͍ͬͯΔՄೳੑ͕͋Δ

    View full-size slide

  30. ·ͱΊ
    Swift 5.9͔Β͸ if ͱ switch Λࣜͱͯ͠ѻ͑ΔΑ͏ʹͳͬͨ
    if / switch ࣜΛ༻͍ͯSwiftUIͷViewΛʢopaque typeͱͯ͠ʣฦ͢ͱ
    Preview࣌ͷϏϧυͰͷΈίϯύΠϧΤϥʔ͕ൃੜ͢Δ
    ҎԼͷ͍ͣΕ͔ͷํ๏Ͱ໰୊ͷରԠΛ͢Δ͜ͱ͕Ͱ͖Δ
    1. @ViewBuilderΩʔϫʔυΛ෇༩͢Δ
    2. Opaque typeͰ͸ͳ͍໌֬ͳܕΛฦ͢ʢText ͳͲʣ
    3. Opaque Type Λ Existential Type ʹஔ͖׵͑Δ

    View full-size slide

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

    View full-size slide