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
Arm移行タイムアタック
qnighy
0
330
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
330
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.2k
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.8k
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
3
690
Jakarta EE meets AI
ivargrimstad
0
200
CSC509 Lecture 09
javiergs
PRO
0
140
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
2k
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
880
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
120
Featured
See All Featured
Optimizing for Happiness
mojombo
376
70k
GitHub's CSS Performance
jonrohan
1030
460k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Typedesign – Prime Four
hannesfritz
40
2.4k
GraphQLとの向き合い方2022年版
quramy
43
13k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Building Applications with DynamoDB
mza
90
6.1k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
How to Ace a Technical Interview
jacobian
276
23k
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 ʹஔ͖͑Δ
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ