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.4k
if 式と switch 式による SwiftUI のプレビューエラー対策
ojun
November 14, 2023
Tweet
Share
More Decks by ojun
See All by ojun
Swift Evolution かるた
ojun9
2
93
Xcode 16のPreviewModifierと@Previewableを活用した効率的なプレビュー方法の考察
ojun9
4
610
Catch Up with Swift 5.10
ojun9
2
750
Overview: Swift OpenAPI Generator
ojun9
3
1.8k
楽しい夏休み!String Catalogs で新しい発見
ojun9
0
2k
ユニットテストを学んだ次に知りたかったApple標準APIに対するテストのやり方
ojun9
1
400
ハッカソンにおけるiOSアプリ開発での学びと楽しさ
ojun9
1
320
Other Decks in Programming
See All in Programming
Security_for_introducing_eBPF
kentatada
0
110
バグを見つけた?それAppleに直してもらおう!
uetyo
0
180
Go の GC の不得意な部分を克服したい
taiyow
2
770
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
1
390
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
190
range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func
mackee
0
110
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
160
testcontainers のススメ
sgash708
1
120
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
700
快速入門可觀測性
blueswen
0
340
17年周年のWebアプリケーションにTanStack Queryを導入する / Implementing TanStack Query in a 17th Anniversary Web Application
saitolume
0
250
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
66
4.5k
The Pragmatic Product Professional
lauravandoore
32
6.3k
Bash Introduction
62gerente
608
210k
Rails Girls Zürich Keynote
gr2m
94
13k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
We Have a Design System, Now What?
morganepeng
51
7.3k
A better future with KSS
kneath
238
17k
Scaling GitHub
holman
458
140k
Designing for humans not robots
tammielis
250
25k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
The World Runs on Bad Software
bkeepers
PRO
65
11k
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 ʹஔ͖͑Δ
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ