Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
if 式と switch 式による SwiftUI のプレビューエラー対策
Search
ojun
November 14, 2023
Programming
1
1.1k
if 式と switch 式による SwiftUI のプレビューエラー対策
ojun
November 14, 2023
Tweet
Share
More Decks by ojun
See All by ojun
Xcode 16のPreviewModifierと@Previewableを活用した効率的なプレビュー方法の考察
ojun9
2
250
Catch Up with Swift 5.10
ojun9
2
690
Overview: Swift OpenAPI Generator
ojun9
3
1.4k
楽しい夏休み!String Catalogs で新しい発見
ojun9
0
1.8k
ユニットテストを学んだ次に知りたかったApple標準APIに対するテストのやり方
ojun9
1
370
ハッカソンにおけるiOSアプリ開発での学びと楽しさ
ojun9
1
310
Other Decks in Programming
See All in Programming
connect-go で面倒くささと戦う / 2024-08-27 #newmo_layerx_go
izumin5210
2
610
ECMAScript仕様を読むのに必要な知識 - ダイジェスト版
syumai
4
2.6k
ゲームボーイアドバンスでSwiftを動かそう
k_koheyi
0
530
2024 컴포즈 정원사
jisungbin
0
150
僕が思い描くTypeScriptの未来を勝手に先取りする
yukukotani
7
2.1k
Why Prism?
kddnewton
4
1.3k
1人で挑むSwiftコンパイラ 〜型システム入門編〜
s_shimotori
0
320
Boost Your Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
1
1.1k
実践 Advanced CallKit 〜快適な通話の実現に向けて〜
mot_techtalk
3
110
マイグレーションコード自作して File-Based Routing に自動移行!! ~250 ページの歴史的経緯を添えて~
cut0
1
250
月間4.5億回再生を超える大規模サービス TVer iOSアプリのリアーキテクチャ戦略 - iOSDC2024
techtver
PRO
1
640
Rubyのobject_id
qnighy
6
1.3k
Featured
See All Featured
Being A Developer After 40
akosma
83
580k
Teambox: Starting and Learning
jrom
131
8.7k
Practical Orchestrator
shlominoach
185
10k
Ruby is Unlike a Banana
tanoku
96
10k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
22
1.7k
Speed Design
sergeychernyshev
19
410
Large-scale JavaScript Application Architecture
addyosmani
508
110k
Build your cross-platform service in a week with App Engine
jlugia
228
18k
How STYLIGHT went responsive
nonsquared
93
5.1k
We Have a Design System, Now What?
morganepeng
48
7.1k
Pencils Down: Stop Designing & Start Developing
hursman
118
11k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
29
2.6k
Transcript
potatotips #85 iOS/Android։ൃTipsڞ༗ձ 2023/11/14 @Voicy if ࣜͱ switch ࣜʹΑΔ SwiftUI
ͷϓϨϏϡʔΤϥʔରࡦ ojun - DeNA Co., Ltd.
ࣗݾհʢojunʣ • name: ojun • 2023৽ଔೖࣾ • iOSΤϯδχΞ @ojun_9
Xcode 15 ରԠਐΜͰ͍·͔͢ʁ
Xcode 15.0 Asset Catalogs / String Catalogs PreviewϚΫϩ Swift 5.9
ϒοΫϚʔΫػೳ ɾෳͷݴޠػೳͷՃ ɾσόοά࣌ʹมͷ༰Λग़ྗ͢ΔͨΊʹར༻͢ΔpίϚϯυͱpoίϚϯυ ʹΑΔग़ྗ͕࣌ؒେ෯ʹॖ ͦͷଞ͍Ζ͍Ζ
Xcode 15.0 Asset Catalogs / String Catalogs PreviewϚΫϩ Swift 5.9
ϒοΫϚʔΫػೳ ɾෳͷݴޠػೳͷՃ ͦͷଞ͍Ζ͍Ζ ɾσόοά࣌ʹมͷ༰Λग़ྗ͢ΔͨΊʹར༻ՄೳͳpίϚϯυͱpoίϚϯυ ʹΑΔग़ྗ͕࣌ؒେ෯ʹॖ
Swift 5.9: if ͱ switch ͷࣜදݱ
Swift 5.9: ifͱswitchͷࣜදݱ Swift 5.9͔ΒswitchͱifΛࣜͱͯ͠දݱͰ͖ΔΑ͏ʹͳͬͨ
Swift 5.9: ifͱswitchͷࣜදݱ Swift 5.9͔ΒswitchͱifΛࣜͱͯ͠දݱͰ͖ΔΑ͏ʹͳͬͨ
Swift 5.9: ifͱswitchͷࣜදݱ Swift 5.9͔ΒswitchͱifΛࣜͱͯ͠දݱͰ͖ΔΑ͏ʹͳͬͨ
Swift 5.9: ifͱswitchͷࣜදݱ Swift 5.9͔ΒswitchͱifΛࣜͱͯ͠දݱͰ͖ΔΑ͏ʹͳͬͨ
Swift 5.9: ifͱswitchͷࣜදݱ Swift 5.9͔ΒswitchͱifΛࣜͱͯ͠දݱͰ͖ΔΑ͏ʹͳͬͨ
Preview࣌ʹར༻͢Δ
Preview࣌ʹར༻͢Δ isSwiftͷBoolʹج͍ͮͯTextΛฦ͢
Preview࣌ʹར༻͢Δ isSwiftͷBoolʹج͍ͮͯTextΛฦ͢ ௨ৗͷϏϧυ 🎉 ͳ͘ޭ͢Δ
Preview࣌ʹར༻͢Δ isSwiftͷBoolʹج͍ͮͯTextΛฦ͢ ௨ৗͷϏϧυ 🎉 ͳ͘ޭ͢Δ Preview࣌ͷϏϧυ 😭 ίϯύΠϧΤϥʔ͕ൃੜ͢Δ
Preview࣌ʹར༻͢Δ isSwiftͷBoolʹج͍ͮͯTextΛฦ͢ ௨ৗͷϏϧυ 🎉 ͳ͘ޭ͢Δ Preview࣌ͷϏϧυ 😭 ίϯύΠϧΤϥʔ͕ൃੜ͢Δ Preview ProviderΛར༻ͯ͠ಉ༷
Preview࣌ʹར༻͢Δ 'if' may only be used as expression in return,
throw, or as the source of an assignment
ղܾํ๏
ղܾํ๏ 1. @ViewBuilderΩʔϫʔυΛ༩͢Δ 2. Opaque typeͰͳ͍໌֬ͳܕΛฦ͢ 3. Opaque Type Λ
Existential Type ʹஔ͖͑Δ
@ViewBuilderΩʔϫʔυΛ༩͢Δ @ViewBuilderΩʔϫʔυΛ༩͢Δ ͜ͱͰPreview࣌ͷΤϥʔΛճආͰ͖Δ ௨ৗͷϏϧυ 🎉 ͳ͘ޭ͢Δ Preview࣌ͷϏϧυ 🎉 ͳ͘ޭ͢Δ
໌֬ͳܕΛฦ͢ ௨ৗͷϏϧυ 🎉 ͳ͘ޭ͢Δ Preview࣌ͷϏϧυ 🎉 ͳ͘ޭ͢Δ `Text`ͷΑ͏ʹܕΛ໌֬ʹ͢Δ ͜ͱͰPreview࣌ͷΤϥʔΛճආͰ͖Δ
Opaque Type Λ Existential Type ʹஔ͖͑Δ ΓΛany Viewʹ͢Δํ๏ ͨͩ͠ɺݺͼग़͠ݩͰܕΩϟετΛ ߦ͏ඞཁ͕͋Δ
௨ৗͷϏϧυ 🎉 ͳ͘ޭ͢Δ Preview࣌ͷϏϧυ 🎉 ͳ͘ޭ͢Δ
ͷݪҼΛߟ͢Δ ʢࠜຊݪҼಥ͖ࢭΊΒΕ·ͤΜͰͨ͠😢ʣ
ͷݪҼΛߟ͢Δ 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
ContentViewͷPreviewϏϧυ͕Δͱ͜ͷϑΝΠϧ͕ߋ৽͞ΕΔ
ྫ͑ɺany ErrorΛฦ͢Α͏ͳϝιουΛ࣮͢Δͱ
ContentView.1.preview-thunk.swift ߋ৽͞ΕΔ
ΓΛOpaque Typeʹ͢Δͱ ͨͩ͠
ContentView.1.preview-thunk.swift ͔ΒgetErrorͷॲཧ͕ফ͑Δ
ͷݪҼΛߟ͢Δ ίϯύΠϧΤϥʔʹͳ࣮ͬͨ Opaque Typeͩͬͨ తͳݪҼ͔Θ͔Βͳ͍͕͜Ε͕ ʹͳ͍ͬͯΔՄೳੑ͕͋Δ
·ͱΊ Swift 5.9͔Β if ͱ switch Λࣜͱͯ͠ѻ͑ΔΑ͏ʹͳͬͨ if / switch
ࣜΛ༻͍ͯSwiftUIͷViewΛʢopaque typeͱͯ͠ʣฦ͢ͱ Preview࣌ͷϏϧυͰͷΈίϯύΠϧΤϥʔ͕ൃੜ͢Δ ҎԼͷ͍ͣΕ͔ͷํ๏ͰͷରԠΛ͢Δ͜ͱ͕Ͱ͖Δ 1. @ViewBuilderΩʔϫʔυΛ༩͢Δ 2. Opaque typeͰͳ͍໌֬ͳܕΛฦ͢ʢText ͳͲʣ 3. Opaque Type Λ Existential Type ʹஔ͖͑Δ
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ