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
Swift Package centered project - Build and Prac...
Search
d_date
September 19, 2021
Programming
20
13k
Swift Package centered project - Build and Practice
Swift Package中心のプロジェクト構成とその実践 - iOSDC JAPAN 2021
https://www.youtube.com/watch?v=e_T1-XrYf4A
d_date
September 19, 2021
Tweet
Share
More Decks by d_date
See All by d_date
TCA Practice in 5 min
d_date
2
1.2k
waiwai-swiftpm-part2
d_date
3
470
わいわいSwift PM part 1
d_date
2
370
What's new in Firebase 2021
d_date
2
1.4k
CI/CDをミニマルに構築する
d_date
1
540
How to write Great Proposal
d_date
4
1.2k
Thinking about Architecture for SwiftUI
d_date
8
2.3k
Integrate your app to modern world in Niigata
d_date
0
620
Integrate your app to modern world
d_date
2
620
Other Decks in Programming
See All in Programming
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
160
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
630
バグを見つけた?それAppleに直してもらおう!
uetyo
0
170
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
250
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
120
Discord Bot with AI -for English learners-
xin9le
1
120
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
3
940
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
170
急成長期の品質とスピードを両立するフロントエンド技術基盤
soarteclab
0
920
From Translations to Multi Dimension Entities
alexanderschranz
2
130
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
770
Featured
See All Featured
Site-Speed That Sticks
csswizardry
2
190
For a Future-Friendly Web
brad_frost
175
9.4k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
How to train your dragon (web standard)
notwaldorf
88
5.7k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
KATA
mclloyd
29
14k
Being A Developer After 40
akosma
87
590k
Facilitating Awesome Meetings
lara
50
6.1k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Into the Great Unknown - MozCon
thekraken
33
1.5k
Statistics for Hackers
jakevdp
796
220k
Transcript
Swift Packageத৺ͷ ϓϩδΣΫτߏͱͦͷ࣮ફ iOSDC JAPAN 2021 Daiki Matsudate @d_date
ٻΉ! Feedback #iosdc #c
🍣 #iosdc #c
લఏͷ֬ೝ Swift Package Manager Xcode Package.swift Multi Module Multi Project
SwiftUI UIKit Bundle Test Target
લఏͷ֬ೝ Swift Package Manager Xcode Package.swift Multi Module Multi Project
SwiftUI UIKit Bundle Test Target Fundamental Advanced
લఏͱͳΔࣝͷ֬ೝ
લఏͱͳΔࣝͷ֬ೝ • Buildʹ͍ͭͯ • Target • Con fi guration •
Scheme • Module / Framework • Import / Link • Static / Dynamic • XCFramework
Build Source Code Object Code Executable Binary Compiler Compile Link
Linker .o Build xcodebuild
Build Object Code DerivedData/*/Build/Intermediates.noindex/*.build/<env>/*.build/Object-normal/<Arch>/
Build Target • Buildʹ༻͍ΔϑΝΠϧʢInput) ੜʢOutput)Λఆٛͨ͠ͷ • Input: Source code, Framework,
Resource • Output: App, App Extension, Framework
Build Configuration • Build SettingsʢϏϧυʹؔ͢Δมͷू߹ʣͷϓϦηοτ
Build Scheme Target Configuration X
Build Scheme • Build5छྨ • Analyze • Test • Run
• Pro fi le • Archive Debug Release
Build Scheme Target Con fi guration Debug Release X Build
Analyze Run Test Pro fi le Archive
Module? Framework?
Module import͕Ͱ͖Δ୯Ґ
Module
Module • Derived Dataʹ.swiftmoduleͱ͍͏Bundle͕ଘࡏ͢Δ͜ͱ ※Bundle … σΟϨΫτϦ͚ͩͲϑΝΠϧͷΑ͏ʹৼΔ͏ܗࣜ • ιʔείʔυ͕ίϯύΠϧ·Ͱ͞ΕΔͱར༻ՄೳʹͳΔ
Module • Import͕Ͱ͖Δ୯Ґ (࣮ମ.swiftmodule) • moduleʹpublic APIؚ͕·ΕΔ • Module Stability
(swift 5.1~) • ҟͳΔSwiftͰੜ͞ΕͨϞδϡʔϧΛimportͰ͖Δʢ.swiftinterface)
Framework Build TargetͱϦϯΫ͢Δ ࣮ߦՄೳόΠφϦ
Framework • Build TargetͱLink͢Δ࣮ߦՄೳόΠφϦ • FrameworkBuild Targetʹ͢Δ͜ͱ͕Ͱ͖ΔʢModuleͰ͖ͳ͍ʣ →Build SchemeΛઃఆͰ͖Δ
FrameworkͷLink • ίϯύΠϧ͞ΕͨObject CodeΛΈ߹Θ͍ͤͯ͘࡞ۀ • Έ߹ΘͤΔࡍʹɺsymbolͷॏෳΛ֬ೝ͢Δ • Linkͷํʹ2छྨ • Static
Link -> Static Framework • Dynamic Link -> Dynamic Framework
Static Link Ϗϧυ࣌ʹObject codeΛͻͱͭʹ·ͱΊ͍ͯ͘ Link Link Link Link .o .o
.o
Load on Launch Dynamic Link ΞϓϦͷόΠφϦͱผʹੜ͠ɺΞϓϦ࣮ߦ࣌ʹಡΈࠐΉ Link Link Link .o
.o .o
Static Link vs Dynamic Link ΞϓϦͱͷLink ىಈ࣌ؒ Static Framework Ϗϧυͷͱ͖
͍ Dynamic Framework ΞϓϦىಈ࣌ ͍
Link in Module LinkModule୯ҐͰߦΘΕΔ Link .o .o .o .o .o
.o .o .o .o
XCFramework • ՄೳͳBundleܗࣜ • ෳͷFramework (ϏϧυࡁΈόΠφϦʣؚ͕·ΕΔ • Platform (iOS, macOS,
tvOS and watchOS) • Simulator / Device • Static / Dynamic • Build Architecture (x86_64, arm64, …)
͜ͷτʔΫͰ͞ͳ͍͜ͱ • Objective-CͷFramework • Module map • Bridging-Header • Library
(Frameworkͱݫີʹ۠ผ͠ͳ͍ʣ • .a • .dylib
https://www.youtube.com/watch?v=FZoYyAEPJ8w iOSDC Japan 2019: ϥΠϒϥϦͷΠϯϙʔτͱϦϯΫͷΈΛ୳Δ / Kishikawa Katsumi See also
Homework https://twitter.com/kateinoigakukun/status/1405130382656163840?s=20 A, B, Cをアプリターゲット、静的ライブラリ、動的ライブラリのいずれかとする。A, B, Cの依存関係 が、 A ->
B, A -> C, C -> Bとなるとき、Aのリンク時にSymbolが重複するかを考える。 A, B, Cが以下の場合、それぞれSymbolが重複するかを答えなさい。 ( 1 ) A: アプリターゲット, B: 静的ライブラリ, C: 動的ライブラリ ( 2 ) A: アプリターゲット, B: 静的ライブラリ, C: 静的ライブラリ ( 3 ) A: アプリターゲット, B: 動的ライブラリ, C: 静的ライブラリ https://twitter.com/kateinoigakukun/status/1405844501680197636?s=20 ग़యɿ ղɿ A B C
͜Ε·Ͱ֬ೝͨ͜͠ͱ • Build • CompileͱLink • Build Target, Con fi
guration, Schemeͷؔ • Moduleͱimport • FrameworkͱLink • Static / Dynamic • XCFramework
Swift Package Manager
None
Swift Package Manager • Pros • swiftʹಉࠝ͞Ε͍ͯΔʢಛʹCIͰؔΘͬͯ͘Δʣ • Xcodeʹ౷߹͞Ε͍ͯΔ •
macOS / Linux྆ରԠ • ࣗಈͰґଘؔͷղܾɺߋ৽ • Cons • Compatible with Swift 5 and Xcode 11
Swift Package Manager in Xcode
None
None
None
8.1.1 Patch Minor Semantic Versioning Major Bug Fix Compatible Additions
Breaking Changes https://semver.org
8.1.1 9.0.0 < Up to Next Major 8.1.1 8.2.0 <
Up to Next Minor
None
None
None
None
None
None
Package.swiftͰ࢝ΊΔ ϓϩδΣΫτϑΝΠϧμΠΤοτ
None
None
None
None
None
None
Package.swift • Products • Library: Build TargetͱLink͢Δ࣮ߦՄೳόΠφϦ (=Framework) • Dependencies
• Targets • Target = Module • Sources/, Tests/ͷαϒσΟϨΫτϦ͕Ϟδϡʔϧ
None
AppFeature FeatureA Firebase FeatureB Link App Import Link Link Import
AppFeature
None
None
None
None
None
None
None
None
None
Package.swift • 3rd partyͷϥΠϒϥϦΛཧ͢Δ͚ͩͰͳ͍ • ModuleFrameworkΛࣗࡏʹߏͰ͖Δ → Multi Modules •
ϓϩδΣΫτϑΝΠϧ͕ܰྔʹ → Multi Projects
XCFramework in Package.swift
XCFramework in Package.swift • ιʔείʔυͰͳ͘όΠφϦܗࣜͰ͍ͯ͠Δ • e.g.) ࠂSDK, AnalyticsͳͲ •
targetʹ.binaryTargetΛ͏ • xcframeworkͦ͘͠ΕΛؚΉzipϑΝΠϧ • ϩʔΧϧͰϦϞʔτͰՄʢϦϞʔτchecksum͕ඞཁʣ
XCFramework in Package.swift
Swift PMʹରԠ͍ͯ͠ͳ͍߹
Swift PMʹରԠ͍ͯ͠ͳ͍߹ • Package.swift͕ͳ͍ → ιʔείʔυ͕ެ։͞Ε͍ͯΕϏϧυ͕ՄೳͳͷͰɺPull RequestΛग़͠ ͯPackage.swiftΛՃɻ͘͠Forkͯ͠Ճ • ιʔείʔυඇެ։ɺXCFrameworkͳ͍ʢFramework͔͠ͳ͍ʣ
→ CocoaPodsΛซ༻͢Δͷ͕खͬऔΓૣ͍ → Apple M1 (arm64) ͷ߹ Intel Mac (x86_64) ͰϏϧυ͞ΕͨFramework ѻ͑ͳ͍ͷͰ͢Δʢޙʹհʣ
Resources in Swift PM
Resources in Swift PM • Swift PMͰAssetsLocalizable.stringऔΓѻ͑Δ • Localize͢Δ߹Package.swiftʹdefaultLocalizationΛ͚ͭΔ
Resources in Swift PM • Bundle.moduleΛͬͯΞΫηε͢Δ
resource_bundle_accessor.swift
Localization in Swift PM Localization for Xcode Preview
Localization in Swift PM Localization for Xcode Preview
Localization in Swift PM Localization for Xcode Preview
Localization in Swift PM Localization for Build Target
Resources in Swift PM Best Practice ✅ϦιʔεϞδϡʔϧຖʹཧ͢Δ • Resource Bundle
Accessor (Bundle.module) ಉ͡ϞδϡʔϧͷϦιʔεʹର ͯ͠༗ޮ ⚠ ͠ଞͷϞδϡʔϧʹఏڙ͢Δඞཁ͕͋Δ߹ݸʑͷϦιʔεʹରͯ͠Typed Public AccessorΛఏڙ͢Δ • Ϧιʔεͷ໊લ͕֎෦ͱͷґଘؔͱͳͬͯ͠·͏ͨΊਪ͞Εͳ͍ 🚫 Resource BundleͷΞΫηεΛAPIʹ͠ͳ͍ https://developer.apple.com/videos/play/wwdc2020/10169
Resources in Swift PM Best Practice https://developer.apple.com/videos/play/wwdc2020/10169
Resources in Swift PM BundleͲ͜ʁ
Swift PM with UIKit
Swift PM with UIKit • Xib, Storyboard͕Ϧιʔεѻ͍ʹͳΔ • Xib, StoryboardͰModuleΛࢦఆ͢Δ
• ࢦఆ͠ͳ͍ͱΫϥογϡ͢Δ • xibStoryboard͔ΒViewΛੜ͢ΔςετΛॻ͘ • IBLinterΛ͏
Swift PM with UIKit Use IBLinter https://github.com/IBDecodable/IBLinter
Multi module in Swift PM
Multi module in Swift PM Pros • ίϯύΠϧ࣌ؒͷॖ • Xcode
Previewͷ҆ఆԽ • App ClipͷΑ͏ͳ༰ྔ੍ݶ • Mini-Application
Mini-Application • ಛఆͷػೳModuleΛબͼɺFrameworkΛߏ • Build TargetʹFrameworkΛՃ͢Δ • ೝূը໘OnboardͳͲɺද͕ࣔ݅͋Δͷʹಛʹ༗ޮ
Multi module in Swift PM Best Practice • Common /
Misc moduleΛ࡞Βͳ͍ • ࡞Γͨ͘ͳͬͨΒͰ͖Δ͚ͩ࠷খ୯Ґʹׂ͢Δ • ResourceͷॏෳΛ͋·ΓڪΕͳ͍ • େ͖ͳը૾Λ৭ΜͳϞδϡʔϧͰ͍͍ͨ߹ͦΕࣗମΛϞδϡʔϧʹ͢ Δ
Multi projects in Swift PM
Multi projects in Swift PM • ϓϩδΣΫτͷׂ͕ബ͘ͳͬͨͷͰEnvironmentͷઃఆʹશৼΓͰ͖Δ • Staging.xcodeproj •
Production.xcodeproj • (Staging-M1.xcodeproj)
Multi projects in Swift PM Build Con fi guration vs
Multi Project • Build Con fi gurationͰཧͰ͖ΔͷBuild Settingsͷม Build SettingsͷUser-De fi nedʹΧελϜͳมΛೖΕΔ • Info.plist͔ΒऔΓग़ͤΔΑ͏ʹ͢Δ
Multi projects in Swift PM Build Con fi guration vs
Multi Project • Xcode ProjectͰཧ͢ΔϑΝΠϧ͔͔ͨͩछྨ • ࢀর͢ΔϑΝΠϧಉ͡ͰҧͬͯΑ͍ • Project͕ҧ͏ͱϑΝΠϧ໊ॏෳͯ͠େৎ • e.g.) GoogleService-info.plist • Build Con fi gurationซ༻Ͱ͖Δ
Multi Projects Case Study • ARM64༻Xcode ProjectΛผͰ࡞Δ • CocoaPodsΛ͜ͷϓϩδΣΫτ͚ͩೖΕͳ͍Α͏ʹ͢Δ •
CocoaPodsʹೖ͍ͬͯΔϥΠϒϥϦimportͰ͖ͳ͘ͳΔͷͰ #if canImportͰճආ͢Δ
ࠓͷ·ͱΊ • ݴ༿ͷఆٛΛઆ໌Ͱ͖ΔΑ͏ʹ • PackageͰιʔείʔυΛཧ͠ɺϓϩδΣΫτϑΝΠϧΛܰ͘͢Δ • ϚϧνϞδϡʔϧͳߏͰϏϧυ࣌ؒͷॖMini Application • ෳͷϓϩδΣΫτϑΝΠϧͷӡ༻Ͱڥઃఆ͕؆୯ʹ
Case Study
GANMA! / FLINTERS, Inc. • UIKit based • CocoaPods /
Carthageͷซ༻ → CocoaPods / SwiftPMͷซ༻ • Ұ෦ͰXCFrameworkʹରԠ͍ͯ͠ͳ͍Pre built binary • Embedded FrameworkΛΊͨͷͰϓϩδΣΫτϑΝΠϧ͕খ͘͞ͳͬ ͨ • ΞϓϦαΠζ: 23.5MB → 20.6MB Case Study
Timee / Timee, inc. • UIKit based • CocoaPods /
Carthageͷซ༻ → CocoaPods / SwiftPMͷซ༻ • Ұ෦ͰXCFrameworkʹରԠ͍ͯ͠ͳ͍Pre built binary • ϓϩδΣΫτϑΝΠϧ͕খ͘͞ͳΓɺXcodeGenΛണ͕ͨ͠ • ηοτΞοϓ͕༰қʹ • ΞϓϦαΠζ: 30.3 MB → 26.9MB Case Study
NOT A HOTEL, Inc. • SwiftUI • Multi modules (16
→ 32 and more !) • Preview Apps • Multi projects • Staging • Production • (Staging-M1 Archived) Case Study
Case Study And you !!
Resources • https://github.com/pointfreeco/isowords • https://www.pointfree.co/episodes/ep142-a-tour-of-isowords-part-1 ʢpart 4·Ͱ͋Δʣ • https://date.notion.site/isowords-8f8982eb3a9a4665b2fa688b06791b70 •
https://date.notion.site/Swift-PM-Build- Con fi guration-4f14ceac795a4338a5a44748adfeaa40
ٻΉ! Feedback
Swift Packageத৺ͷ ϓϩδΣΫτߏͱͦͷ࣮ફ iOSDC JAPAN 2021 Daiki Matsudate @d_date