Swift code reuse
made simple
modules, packages, libraries
Slide 2
Slide 2 text
@aronskaya
• macOS Engineer at MacPaw
• CleanMyMac
Slide 3
Slide 3 text
Hoover
Slide 4
Slide 4 text
Fixel
Slide 5
Slide 5 text
@aronskaya
• macOS Chapter Lead at
Women Who Code Kyiv
• workshop mentor at SwiftAveiro
• one of founders of MacPaw Tech Talks
Slide 6
Slide 6 text
Swift code reuse
made simple
modules, packages, libraries
Slide 7
Slide 7 text
No content
Slide 8
Slide 8 text
Behind this
talk
1⃣ projects with 1 target
Slide 9
Slide 9 text
Behind this
talk
1⃣
projects with 1 target
apps, containing several apps
Slide 10
Slide 10 text
Behid this
talk
1⃣
projects with 1 target
apps, containing several apps
C++ libraries, shared with Linux
Slide 11
Slide 11 text
Behid this
talk
1⃣
projects with 1 target
apps, containing several apps
C++ libraries, shared with Linux
projects with dozens internal libs
Slide 12
Slide 12 text
1 app == 27 projects
up to 14 targets per project
Slide 13
Slide 13 text
Agenda
⁉ Why separate codebase
Slide 14
Slide 14 text
Agenda
⁉ Why separate codebase
How to do it: static, dylibs, frameworks, modules
Slide 15
Slide 15 text
Agenda
⁉ Why separate codebase
How to do it: static, dylibs, frameworks, modules
Dependency management: CocoaPods, Swift Package Manager
Slide 16
Slide 16 text
Agenda
⁉ Why separate codebase
How to do it: static, dylibs, frameworks, modules
Takeaways: best practices & modern approach
Dependency management: CocoaPods, Swift Package Manager
Slide 17
Slide 17 text
Why
Slide 18
Slide 18 text
No content
Slide 19
Slide 19 text
No content
Slide 20
Slide 20 text
No content
Slide 21
Slide 21 text
Faster
compilation
Slide 22
Slide 22 text
Faster
app launch
Slide 23
Slide 23 text
Smaller
app bundle
Slide 24
Slide 24 text
⌚ Easily
support new
platforms
Slide 25
Slide 25 text
Grow
your GitHub
Slide 26
Slide 26 text
Test once
use again and again
Slide 27
Slide 27 text
Learn&Train
to think bigger
Slide 28
Slide 28 text
How Apple does it
Slide 29
Slide 29 text
How Apple does it
Slide 30
Slide 30 text
How Apple does it
172
Slide 31
Slide 31 text
Programmer evolution
Slide 32
Slide 32 text
Programmer evolution
it works!
Slide 33
Slide 33 text
Programmer evolution
it works!
it works & is grouped in classes
Slide 34
Slide 34 text
Programmer evolution
it works!
it works & is grouped in classes
it works & is grouped in reusable classes
Slide 35
Slide 35 text
Programmer evolution
it works!
it works & is grouped in classes
it works & is grouped in reusable classes
it works & grouped in a reusable module
Slide 36
Slide 36 text
The Versus
Quiz
Slide 37
Slide 37 text
Static
Dynamic
Slide 38
Slide 38 text
Static
linking / libraries
Slide 39
Slide 39 text
Static library
Slide 40
Slide 40 text
Amazing Notes app
Slide 41
Slide 41 text
Amazing Notes app
Slide 42
Slide 42 text
No content
Slide 43
Slide 43 text
Dynamic
linking / libraries
Slide 44
Slide 44 text
Dynamic library
dylib
Slide 45
Slide 45 text
Library
Framework
Slide 46
Slide 46 text
No content
Slide 47
Slide 47 text
No content
Slide 48
Slide 48 text
No content
Slide 49
Slide 49 text
Library
Conceptually Technically
❌ doesn't affect the lifecycle of
our app or objects
1⃣ a file (+ header files)
can be dynamic or static
Conceptual examples:
CocoaLumberjack, SDWebImage,
AFNetworking
Technical example: stdlib
Slide 50
Slide 50 text
dylib
Slide 51
Slide 51 text
Framework
Conceptually Technically
↺ affects the lifecycle of our app or
objects
a directory with at least 1 dylib
is basically a dylib with bells &
whistles
Example: UIKit (we build our logic based on delegate callbacks—
SceneDelegate, AppDelegate, UITableViewDelegate
Slide 52
Slide 52 text
No content
Slide 53
Slide 53 text
Recap
Static library
headers + code, embedded into client's
executable
Dynamic library headers + code in a separate file
Framework dynamic library + resources
Slide 54
Slide 54 text
Library
Module
Slide 55
Slide 55 text
–Access Control—The Swift Programming Language docs
“A module is a single unit of code distribution—a
framework or application that is built and shipped as a
single unit and that can be imported by another
module with Swift’s import keyword.”
Slide 56
Slide 56 text
–Access Control—The Swift Programming Language docs
“A module is a single unit of code distribution—a
framework or application that is built and shipped as a
single unit and that can be imported by another
module with Swift’s import keyword.”
Slide 57
Slide 57 text
Module
is just another type of header
Slide 58
Slide 58 text
❌Implementation
Slide 59
Slide 59 text
❌Implementation
The impl lives
in a static or a dynamic library
Slide 60
Slide 60 text
2 Types of Modules
Slide 61
Slide 61 text
2 Types of Modules
Clang module
•Objective-C, Swift…
•module.modulemap
Slide 62
Slide 62 text
2 Types of Modules
Clang module Swift module
•Objective-C, Swift
•module.modulemap
•Swift only
•.swiftdoc, .swiftmodule
Slide 63
Slide 63 text
Swift Package Manager
CocoaPods
Slide 64
Slide 64 text
SPM vs CocoaPods
Swift ✅
Objective-C
Xcode integration
Multi platform support
Resources
Slide 65
Slide 65 text
SPM vs CocoaPods
Swift ✅ ✅
Objective-C
Xcode integration
Multi platform support
Resources
Slide 66
Slide 66 text
SPM vs CocoaPods
Swift ✅ ✅
Objective-C ✅
Xcode integration
Multi platform support
Resources
Slide 67
Slide 67 text
SPM vs CocoaPods
Swift ✅ ✅
Objective-C ✅ ⏳
Xcode integration
Multi platform support
Resources
Slide 68
Slide 68 text
SPM vs CocoaPods
Swift ✅ ✅
Objective-C ✅ ⏳
Xcode integration ❌
Multi platform support
Resources
Slide 69
Slide 69 text
SPM vs CocoaPods
Swift ✅ ✅
Objective-C ✅ ⏳
Xcode integration ❌ ✅
Multi platform support
Resources
Slide 70
Slide 70 text
SPM vs CocoaPods
Swift ✅ ✅
Objective-C ✅ ⏳
Xcode integration ❌ ✅
Multi platform support ❌
Resources
Slide 71
Slide 71 text
SPM vs CocoaPods
Swift ✅ ✅
Objective-C ✅ ⏳
Xcode integration ❌ ✅
Multi platform support ❌ ✅
Resources
Slide 72
Slide 72 text
SPM vs CocoaPods
Swift ✅ ✅
Objective-C ✅ ⏳
Xcode integration ❌ ✅
Multi platform support ❌ ✅
Resources ✅
Slide 73
Slide 73 text
SPM vs CocoaPods
Swift ✅ ✅
Objective-C ✅ ⏳
Xcode integration ❌ ✅
Multi platform support ❌ ✅
Resources ✅ ⏳
Slide 74
Slide 74 text
No content
Slide 75
Slide 75 text
–Serg Krivoblotsky, my college
“I think, SPM is the most convenient Dependency
Manager on the market. And Xcode integration makes
it a dream. You paste a GitHub link—and you're in
game!”
Slide 76
Slide 76 text
–Serg Krivoblotsky, my college
“I think, SPM is the most convenient Dependency
Manager on the market. And Xcode integration makes
it a dream. You paste a GitHub link—and you're in
game!”
Triggers: when separate code
1. You have files included in
several targets
Slide 89
Slide 89 text
Triggers: when separate code
1. You have files included in
several targets
2. You have a group of classes,
that work together to provide a
single piece of functionality
Slide 90
Slide 90 text
What to put in a separate lib?
•Networking logic
Slide 91
Slide 91 text
What to put in a separate lib?
•Networking logic
•Foundation enhancements
Slide 92
Slide 92 text
What to put in a separate lib?
•Networking logic
•Foundation enhancements
•Trial limitations
Slide 93
Slide 93 text
What to put in a separate lib?
•Networking logic
•Foundation enhancements
•Trial limitations
•Working with database
Slide 94
Slide 94 text
What to put in a separate lib?
•Networking logic
•Foundation enhancements
•Trial limitations
•Working with database
• Analytics
Slide 95
Slide 95 text
What to put in a separate lib?
•Networking logic
•Foundation enhancements
•Trial limitations
•Working with database
• Analytics
•Every new feature
Slide 96
Slide 96 text
It is to have
a library per
each feature
Slide 97
Slide 97 text
It is to have
a library per
each feature
Not 70
Slide 98
Slide 98 text
Tips on designing an interface
• Allow flexible configuration (pass it in parameters, don’t
try to guess what the client wants)
init(param1:param2:param3:param4:)
Slide 99
Slide 99 text
Tips on designing an interface
• Allow also simple default setup (have little or no
parameters)
init()
Slide 100
Slide 100 text
Tips on designing an interface
• Allow asynchronous execution
func execute(param0:param1:completionHandler:)
Slide 101
Slide 101 text
Tips on designing an interface
• Use Apple Frameworks as example
func tableView(_ tableView: UITableView,
didDeselectRowAt indexPath: IndexPath)
Slide 102
Slide 102 text
Tips on designing an interface
• Use famous and popular libraries and frameworks as examples
Slide 103
Slide 103 text
What to use
if I start a project today?
Slide 104
Slide 104 text
What to use
if I start a project today?
Frameworks
Slide 105
Slide 105 text
What to use
if I start a project today?
Swift Package Manager