Slide 1

Slide 1 text

Swift Packageத৺ͷ ϓϩδΣΫτߏ੒ͱͦͷ࣮ફ iOSDC JAPAN 2021 Daiki Matsudate @d_date

Slide 2

Slide 2 text

ٻΉ! Feedback #iosdc #c

Slide 3

Slide 3 text

🍣 #iosdc #c

Slide 4

Slide 4 text

લఏͷ֬ೝ Swift Package Manager Xcode Package.swift Multi Module Multi Project SwiftUI UIKit Bundle Test Target

Slide 5

Slide 5 text

લఏͷ֬ೝ Swift Package Manager Xcode Package.swift Multi Module Multi Project SwiftUI UIKit Bundle Test Target Fundamental Advanced

Slide 6

Slide 6 text

લఏͱͳΔ஌ࣝͷ֬ೝ

Slide 7

Slide 7 text

લఏͱͳΔ஌ࣝͷ֬ೝ • Buildʹ͍ͭͯ • Target • Con fi guration • Scheme • Module / Framework • Import / Link • Static / Dynamic • XCFramework

Slide 8

Slide 8 text

Build Source Code Object Code Executable Binary Compiler Compile Link Linker .o Build xcodebuild

Slide 9

Slide 9 text

Build Object Code DerivedData/*/Build/Intermediates.noindex/*.build//*.build/Object-normal//

Slide 10

Slide 10 text

Build Target • Buildʹ༻͍ΔϑΝΠϧʢInput) ΍ੜ੒෺ʢOutput)Λఆٛͨ͠΋ͷ • Input: Source code, Framework, Resource • Output: App, App Extension, Framework

Slide 11

Slide 11 text

Build Configuration • Build SettingsʢϏϧυʹؔ͢Δม਺ͷू߹ʣͷϓϦηοτ

Slide 12

Slide 12 text

Build Scheme Target Configuration X

Slide 13

Slide 13 text

Build Scheme • Build͸5छྨ • Analyze • Test • Run • Pro fi le • Archive Debug Release

Slide 14

Slide 14 text

Build Scheme Target Con fi guration Debug Release X Build Analyze Run Test Pro fi le Archive

Slide 15

Slide 15 text

Module? Framework?

Slide 16

Slide 16 text

Module import͕Ͱ͖Δ୯Ґ

Slide 17

Slide 17 text

Module

Slide 18

Slide 18 text

Module • Derived Dataʹ.swiftmoduleͱ͍͏Bundle͕ଘࡏ͢Δ͜ͱ
 ※Bundle … σΟϨΫτϦ͚ͩͲϑΝΠϧͷΑ͏ʹৼΔ෣͏ܗࣜ • ιʔείʔυ͕ίϯύΠϧ·Ͱ͞ΕΔͱར༻ՄೳʹͳΔ

Slide 19

Slide 19 text

Module • Import͕Ͱ͖Δ୯Ґ (࣮ମ͸.swiftmodule) • moduleʹ͸public APIؚ͕·ΕΔ • Module Stability (swift 5.1~) • ҟͳΔSwiftͰੜ੒͞ΕͨϞδϡʔϧΛimportͰ͖Δʢ.swiftinterface)

Slide 20

Slide 20 text

Framework Build TargetͱϦϯΫ͢Δ ࣮ߦՄೳόΠφϦ

Slide 21

Slide 21 text

Framework • Build TargetͱLink͢Δ࣮ߦՄೳόΠφϦ • Framework͸Build Targetʹ͢Δ͜ͱ͕Ͱ͖ΔʢModule͸Ͱ͖ͳ͍ʣ
 →Build SchemeΛઃఆͰ͖Δ

Slide 22

Slide 22 text

FrameworkͷLink • ίϯύΠϧ͞ΕͨObject CodeΛ૊Έ߹Θ͍ͤͯ͘࡞ۀ • ૊Έ߹ΘͤΔࡍʹɺsymbolͷॏෳΛ֬ೝ͢Δ • Linkͷ࢓ํʹ͸2छྨ • Static Link -> Static Framework • Dynamic Link -> Dynamic Framework

Slide 23

Slide 23 text

Static Link Ϗϧυ࣌ʹObject codeΛͻͱͭʹ·ͱΊ͍ͯ͘ Link Link Link Link .o .o .o

Slide 24

Slide 24 text

Load on Launch Dynamic Link ΞϓϦͷόΠφϦͱผʹੜ੒͠ɺΞϓϦ࣮ߦ࣌ʹಡΈࠐΉ Link Link Link .o .o .o

Slide 25

Slide 25 text

Static Link vs Dynamic Link ΞϓϦͱͷLink ىಈ࣌ؒ Static Framework Ϗϧυͷͱ͖ ୹͍ Dynamic Framework ΞϓϦىಈ࣌ ௕͍

Slide 26

Slide 26 text

Link in Module Link͸Module୯ҐͰߦΘΕΔ Link .o .o .o .o .o .o .o .o .o

Slide 27

Slide 27 text

XCFramework • ഑෍ՄೳͳBundleܗࣜ • ෳ਺ͷFramework (ϏϧυࡁΈόΠφϦʣؚ͕·ΕΔ • Platform (iOS, macOS, tvOS and watchOS) • Simulator / Device • Static / Dynamic • Build Architecture (x86_64, arm64, …)

Slide 28

Slide 28 text

͜ͷτʔΫͰ࿩͞ͳ͍͜ͱ • Objective-C੡ͷFramework • Module map • Bridging-Header • Library (Frameworkͱݫີʹ۠ผ͠ͳ͍ʣ • .a • .dylib

Slide 29

Slide 29 text

https://www.youtube.com/watch?v=FZoYyAEPJ8w iOSDC Japan 2019: ϥΠϒϥϦͷΠϯϙʔτͱϦϯΫͷ࢓૊ΈΛ୳Δ / Kishikawa Katsumi See also

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

͜Ε·Ͱ֬ೝͨ͜͠ͱ • Build • CompileͱLink • Build Target, Con fi guration, Schemeͷؔ܎ • Moduleͱimport • FrameworkͱLink • Static / Dynamic • XCFramework

Slide 32

Slide 32 text

Swift Package Manager

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Swift Package Manager • Pros • swiftʹಉࠝ͞Ε͍ͯΔʢಛʹCIͰؔΘͬͯ͘Δʣ • Xcodeʹ౷߹͞Ε͍ͯΔ • macOS / Linux྆ରԠ • ࣗಈͰґଘؔ܎ͷղܾɺߋ৽ • Cons • Compatible with Swift 5 and Xcode 11

Slide 35

Slide 35 text

Swift Package Manager in Xcode

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

8.1.1 Patch Minor Semantic Versioning Major Bug Fix Compatible Additions Breaking Changes https://semver.org

Slide 40

Slide 40 text

8.1.1 9.0.0 < Up to Next Major 8.1.1 8.2.0 < Up to Next Minor

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Package.swiftͰ࢝ΊΔ ϓϩδΣΫτϑΝΠϧμΠΤοτ

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Package.swift • Products • Library: Build TargetͱLink͢Δ࣮ߦՄೳόΠφϦ (=Framework) • Dependencies • Targets • Target = Module • Sources/, Tests/ͷαϒσΟϨΫτϦ͕Ϟδϡʔϧ

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

AppFeature FeatureA Firebase FeatureB Link App Import Link Link Import AppFeature

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

Package.swift • 3rd partyͷϥΠϒϥϦΛ؅ཧ͢Δ͚ͩͰ͸ͳ͍ • Module΍FrameworkΛࣗࡏʹߏ੒Ͱ͖Δ → Multi Modules • ϓϩδΣΫτϑΝΠϧ͕ܰྔʹ → Multi Projects

Slide 67

Slide 67 text

XCFramework in Package.swift

Slide 68

Slide 68 text

XCFramework in Package.swift • ιʔείʔυͰ͸ͳ͘όΠφϦܗࣜͰ഑෍͍ͯ͠Δ • e.g.) ޿ࠂSDK, AnalyticsͳͲ • targetʹ.binaryTargetΛ࢖͏ • xcframework΋͘͠͸ͦΕΛؚΉzipϑΝΠϧ • ϩʔΧϧͰ΋ϦϞʔτͰ΋ՄʢϦϞʔτ͸checksum͕ඞཁʣ

Slide 69

Slide 69 text

XCFramework in Package.swift

Slide 70

Slide 70 text

Swift PMʹରԠ͍ͯ͠ͳ͍৔߹

Slide 71

Slide 71 text

Swift PMʹରԠ͍ͯ͠ͳ͍৔߹ • Package.swift͕ͳ͍
 → ιʔείʔυ͕ެ։͞Ε͍ͯΕ͹Ϗϧυ͕ՄೳͳͷͰɺPull RequestΛग़͠ ͯPackage.swiftΛ௥Ճɻ΋͘͠͸Forkͯ͠௥Ճ • ιʔείʔυඇެ։ɺXCFramework΋ͳ͍ʢFramework͔͠ͳ͍ʣ
 → CocoaPodsΛซ༻͢Δͷ͕खͬऔΓૣ͍
 → Apple M1 (arm64) ͷ৔߹͸ Intel Mac (x86_64) ͰϏϧυ͞ΕͨFramework ͸ѻ͑ͳ͍ͷͰ޻෉͢Δʢޙ൒ʹ঺հʣ

Slide 72

Slide 72 text

Resources in Swift PM

Slide 73

Slide 73 text

Resources in Swift PM • Swift PMͰ͸Assets΍Localizable.string΋औΓѻ͑Δ • Localize͢Δ৔߹͸Package.swiftʹdefaultLocalizationΛ͚ͭΔ

Slide 74

Slide 74 text

Resources in Swift PM • Bundle.moduleΛ࢖ͬͯΞΫηε͢Δ

Slide 75

Slide 75 text

resource_bundle_accessor.swift

Slide 76

Slide 76 text

Localization in Swift PM Localization for Xcode Preview

Slide 77

Slide 77 text

Localization in Swift PM Localization for Xcode Preview

Slide 78

Slide 78 text

Localization in Swift PM Localization for Xcode Preview

Slide 79

Slide 79 text

Localization in Swift PM Localization for Build Target

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

Resources in Swift PM Best Practice https://developer.apple.com/videos/play/wwdc2020/10169

Slide 82

Slide 82 text

Resources in Swift PM Bundle͸Ͳ͜΁ʁ

Slide 83

Slide 83 text

Swift PM with UIKit

Slide 84

Slide 84 text

Swift PM with UIKit • Xib, Storyboard͕Ϧιʔεѻ͍ʹͳΔ • Xib, Storyboard಺ͰModuleΛࢦఆ͢Δ • ࢦఆ͠ͳ͍ͱΫϥογϡ͢Δ • xib΍Storyboard͔ΒViewΛੜ੒͢ΔςετΛॻ͘ • IBLinterΛ࢖͏

Slide 85

Slide 85 text

Swift PM with UIKit Use IBLinter https://github.com/IBDecodable/IBLinter

Slide 86

Slide 86 text

Multi module in Swift PM

Slide 87

Slide 87 text

Multi module in Swift PM Pros • ίϯύΠϧ࣌ؒͷ୹ॖ • Xcode Previewͷ҆ఆԽ • App ClipͷΑ͏ͳ༰ྔ੍ݶ • Mini-Application

Slide 88

Slide 88 text

Mini-Application • ಛఆͷػೳModuleΛબͼɺFrameworkΛߏ੒ • Build TargetʹFrameworkΛ௥Ճ͢Δ • ೝূը໘΍OnboardͳͲɺදࣔ৚͕݅͋Δ΋ͷʹಛʹ༗ޮ

Slide 89

Slide 89 text

Multi module in Swift PM Best Practice • Common / Misc moduleΛ࡞Βͳ͍ • ࡞Γͨ͘ͳͬͨΒͰ͖Δ͚ͩ࠷খ୯Ґʹ෼ׂ͢Δ • ResourceͷॏෳΛ͋·ΓڪΕͳ͍ • େ͖ͳը૾Λ৭ΜͳϞδϡʔϧͰ࢖͍͍ͨ৔߹͸ͦΕࣗମΛϞδϡʔϧʹ͢ Δ

Slide 90

Slide 90 text

Multi projects in Swift PM

Slide 91

Slide 91 text

Multi projects in Swift PM • ϓϩδΣΫτͷ໾ׂ͕ബ͘ͳͬͨͷͰEnvironmentͷઃఆʹશৼΓͰ͖Δ • Staging.xcodeproj • Production.xcodeproj • (Staging-M1.xcodeproj)

Slide 92

Slide 92 text

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͔ΒऔΓग़ͤΔΑ͏ʹ͢Δ

Slide 93

Slide 93 text

Multi projects in Swift PM Build Con fi guration vs Multi Project • Xcode ProjectͰ؅ཧ͢ΔϑΝΠϧ͸͔͔ͨͩ਺छྨ • ࢀর͢ΔϑΝΠϧ͸ಉ͡Ͱ΋ҧͬͯ΋Α͍
 • Project͕ҧ͏ͱϑΝΠϧ໊͸ॏෳͯ͠΋େৎ෉ • e.g.) GoogleService-info.plist • Build Con fi guration΋ซ༻Ͱ͖Δ

Slide 94

Slide 94 text

Multi Projects Case Study • ARM64༻Xcode ProjectΛผͰ࡞Δ • CocoaPodsΛ͜ͷϓϩδΣΫτ͚ͩೖΕͳ͍Α͏ʹ͢Δ • CocoaPodsʹೖ͍ͬͯΔϥΠϒϥϦ͸importͰ͖ͳ͘ͳΔͷͰ
 #if canImportͰճආ͢Δ

Slide 95

Slide 95 text

ࠓ೔ͷ·ͱΊ • ݴ༿ͷఆٛΛઆ໌Ͱ͖ΔΑ͏ʹ • PackageͰιʔείʔυΛ؅ཧ͠ɺϓϩδΣΫτϑΝΠϧΛܰ͘͢Δ • ϚϧνϞδϡʔϧͳߏ੒ͰϏϧυ࣌ؒͷ୹ॖ΍Mini Application • ෳ਺ͷϓϩδΣΫτϑΝΠϧͷӡ༻Ͱ؀ڥઃఆ͕؆୯ʹ

Slide 96

Slide 96 text

Case Study

Slide 97

Slide 97 text

GANMA! / FLINTERS, Inc. • UIKit based • CocoaPods / Carthageͷซ༻ → CocoaPods / SwiftPMͷซ༻ • Ұ෦ͰXCFrameworkʹରԠ͍ͯ͠ͳ͍Pre built binary • Embedded FrameworkΛ΍ΊͨͷͰϓϩδΣΫτϑΝΠϧ͕খ͘͞ͳͬ ͨ • ΞϓϦαΠζ: 23.5MB → 20.6MB Case Study

Slide 98

Slide 98 text

Timee / Timee, inc. • UIKit based • CocoaPods / Carthageͷซ༻ → CocoaPods / SwiftPMͷซ༻ • Ұ෦ͰXCFrameworkʹରԠ͍ͯ͠ͳ͍Pre built binary • ϓϩδΣΫτϑΝΠϧ͕খ͘͞ͳΓɺXcodeGenΛണ͕ͨ͠ • ηοτΞοϓ͕༰қʹ • ΞϓϦαΠζ: 30.3 MB → 26.9MB Case Study

Slide 99

Slide 99 text

NOT A HOTEL, Inc. • SwiftUI • Multi modules (16 → 32 and more !) • Preview Apps • Multi projects • Staging • Production • (Staging-M1 Archived) Case Study

Slide 100

Slide 100 text

Case Study And you !!

Slide 101

Slide 101 text

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

Slide 102

Slide 102 text

ٻΉ! Feedback

Slide 103

Slide 103 text

Swift Packageத৺ͷ ϓϩδΣΫτߏ੒ͱͦͷ࣮ફ iOSDC JAPAN 2021 Daiki Matsudate @d_date