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.1k
waiwai-swiftpm-part2
d_date
3
460
わいわいSwift PM part 1
d_date
2
370
What's new in Firebase 2021
d_date
2
1.4k
CI/CDをミニマルに構築する
d_date
1
530
How to write Great Proposal
d_date
4
1.1k
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
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
600
EventSourcingの理想と現実
wenas
6
2.3k
色々なIaCツールを実際に触って比較してみる
iriikeita
0
330
CSC509 Lecture 11
javiergs
PRO
0
180
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
Quine, Polyglot, 良いコード
qnighy
4
640
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.1k
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.4k
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
190
距離関数を極める! / SESSIONS 2024
gam0022
0
280
Featured
See All Featured
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
The Invisible Side of Design
smashingmag
298
50k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
Facilitating Awesome Meetings
lara
50
6.1k
GraphQLとの向き合い方2022年版
quramy
43
13k
How STYLIGHT went responsive
nonsquared
95
5.2k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Scaling GitHub
holman
458
140k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
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