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
創造的活動から切り拓く新たなキャリア 好きから始めてみる夜勤オペレーターからSREへの転身
yjszk
1
130
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
180
talk-with-local-llm-with-web-streams-api
kbaba1001
0
180
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
290
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
200
php-conference-japan-2024
tasuku43
0
270
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
770
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
3
700
Go の GC の不得意な部分を克服したい
taiyow
3
780
testcontainers のススメ
sgash708
1
120
命名をリントする
chiroruxx
1
410
Featured
See All Featured
How GitHub (no longer) Works
holman
311
140k
Automating Front-end Workflow
addyosmani
1366
200k
Designing for Performance
lara
604
68k
How to Ace a Technical Interview
jacobian
276
23k
Writing Fast Ruby
sferik
628
61k
The Invisible Side of Design
smashingmag
298
50k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Code Review Best Practice
trishagee
65
17k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
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