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.3k
if 式と switch 式による SwiftUI のプレビューエラー対策
ojun
November 14, 2023
Tweet
Share
More Decks by ojun
See All by ojun
Swift Evolution かるた
ojun9
2
90
Xcode 16のPreviewModifierと@Previewableを活用した効率的なプレビュー方法の考察
ojun9
4
530
Catch Up with Swift 5.10
ojun9
2
740
Overview: Swift OpenAPI Generator
ojun9
3
1.7k
楽しい夏休み!String Catalogs で新しい発見
ojun9
0
1.9k
ユニットテストを学んだ次に知りたかったApple標準APIに対するテストのやり方
ojun9
1
390
ハッカソンにおけるiOSアプリ開発での学びと楽しさ
ojun9
1
320
Other Decks in Programming
See All in Programming
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
100
受け取る人から提供する人になるということ
little_rubyist
0
230
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
100
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
170
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
330
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
110
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.1k
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
860
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Designing for Performance
lara
604
68k
Site-Speed That Sticks
csswizardry
0
24
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Building Adaptive Systems
keathley
38
2.3k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
120
Navigating Team Friction
lara
183
14k
GitHub's CSS Performance
jonrohan
1030
460k
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 ʹஔ͖͑Δ
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ