Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Code Reuse Made Simple

vashchenko
November 15, 2019

Swift Code Reuse Made Simple

What is the difference between static and dynamic libraries? Between libraries and frameworks? Between libraries and modules? How to manage the code by splitting it reusable chunks?
These and some other questions are answered in this presentation.

Presented at SwiftHeroes 2019 in Italy, Turin, on 15th of November.

vashchenko

November 15, 2019
Tweet

More Decks by vashchenko

Other Decks in Programming

Transcript

  1. Swift code reuse
    made simple
    modules, packages, libraries

    View Slide

  2. @aronskaya
    • macOS Engineer at MacPaw
    • CleanMyMac

    View Slide

  3. Hoover

    View Slide

  4. Fixel

    View Slide

  5. @aronskaya
    • macOS Chapter Lead at
    Women Who Code Kyiv
    • workshop mentor at SwiftAveiro
    • one of founders of MacPaw Tech Talks

    View Slide

  6. Swift code reuse
    made simple
    modules, packages, libraries

    View Slide

  7. View Slide

  8. Behind this
    talk
    1⃣ projects with 1 target

    View Slide

  9. Behind this
    talk
    1⃣

    projects with 1 target
    apps, containing several apps

    View Slide

  10. Behid this
    talk
    1⃣


    projects with 1 target
    apps, containing several apps
    C++ libraries, shared with Linux

    View Slide

  11. Behid this
    talk
    1⃣



    projects with 1 target
    apps, containing several apps
    C++ libraries, shared with Linux
    projects with dozens internal libs

    View Slide

  12. 1 app == 27 projects
    up to 14 targets per project

    View Slide

  13. Agenda
    ⁉ Why separate codebase

    View Slide

  14. Agenda
    ⁉ Why separate codebase
    How to do it: static, dylibs, frameworks, modules

    View Slide

  15. Agenda
    ⁉ Why separate codebase
    How to do it: static, dylibs, frameworks, modules
    Dependency management: CocoaPods, Swift Package Manager

    View Slide

  16. Agenda
    ⁉ Why separate codebase
    How to do it: static, dylibs, frameworks, modules
    Takeaways: best practices & modern approach
    Dependency management: CocoaPods, Swift Package Manager

    View Slide

  17. Why

    View Slide

  18. View Slide

  19. View Slide

  20. View Slide

  21. Faster
    compilation

    View Slide

  22. Faster
    app launch

    View Slide

  23. Smaller
    app bundle

    View Slide

  24. ⌚ Easily
    support new
    platforms

    View Slide

  25. Grow
    your GitHub

    View Slide

  26. Test once
    use again and again

    View Slide

  27. Learn&Train
    to think bigger

    View Slide

  28. How Apple does it

    View Slide

  29. How Apple does it

    View Slide

  30. How Apple does it
    172

    View Slide

  31. Programmer evolution

    View Slide

  32. Programmer evolution
    it works!

    View Slide

  33. Programmer evolution
    it works!

    it works & is grouped in classes


    View Slide

  34. Programmer evolution
    it works!

    it works & is grouped in classes
    it works & is grouped in reusable classes




    View Slide

  35. Programmer evolution
    it works!

    it works & is grouped in classes
    it works & is grouped in reusable classes
    it works & grouped in a reusable module








    View Slide

  36. The Versus
    Quiz

    View Slide

  37. Static
    Dynamic

    View Slide

  38. Static
    linking / libraries

    View Slide

  39. Static library

    View Slide

  40. Amazing Notes app

    View Slide

  41. Amazing Notes app

    View Slide

  42. View Slide

  43. Dynamic
    linking / libraries

    View Slide

  44. Dynamic library
    dylib

    View Slide

  45. Library
    Framework

    View Slide

  46. View Slide

  47. View Slide

  48. View Slide

  49. 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

    View Slide

  50. dylib

    View Slide

  51. 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

    View Slide

  52. View Slide

  53. Recap
    Static library
    headers + code, embedded into client's
    executable
    Dynamic library headers + code in a separate file
    Framework dynamic library + resources

    View Slide

  54. Library
    Module

    View Slide

  55. –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.”

    View Slide

  56. –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.”

    View Slide

  57. Module
    is just another type of header

    View Slide

  58. ❌Implementation

    View Slide

  59. ❌Implementation
    The impl lives
    in a static or a dynamic library

    View Slide

  60. 2 Types of Modules

    View Slide

  61. 2 Types of Modules
    Clang module
    •Objective-C, Swift…
    •module.modulemap

    View Slide

  62. 2 Types of Modules
    Clang module Swift module
    •Objective-C, Swift
    •module.modulemap
    •Swift only
    •.swiftdoc, .swiftmodule

    View Slide

  63. Swift Package Manager
    CocoaPods

    View Slide

  64. SPM vs CocoaPods
    Swift ✅
    Objective-C
    Xcode integration
    Multi platform support
    Resources

    View Slide

  65. SPM vs CocoaPods
    Swift ✅ ✅
    Objective-C
    Xcode integration
    Multi platform support
    Resources

    View Slide

  66. SPM vs CocoaPods
    Swift ✅ ✅
    Objective-C ✅
    Xcode integration
    Multi platform support
    Resources

    View Slide

  67. SPM vs CocoaPods
    Swift ✅ ✅
    Objective-C ✅ ⏳
    Xcode integration
    Multi platform support
    Resources

    View Slide

  68. SPM vs CocoaPods
    Swift ✅ ✅
    Objective-C ✅ ⏳
    Xcode integration ❌
    Multi platform support
    Resources

    View Slide

  69. SPM vs CocoaPods
    Swift ✅ ✅
    Objective-C ✅ ⏳
    Xcode integration ❌ ✅
    Multi platform support
    Resources

    View Slide

  70. SPM vs CocoaPods
    Swift ✅ ✅
    Objective-C ✅ ⏳
    Xcode integration ❌ ✅
    Multi platform support ❌
    Resources

    View Slide

  71. SPM vs CocoaPods
    Swift ✅ ✅
    Objective-C ✅ ⏳
    Xcode integration ❌ ✅
    Multi platform support ❌ ✅
    Resources

    View Slide

  72. SPM vs CocoaPods
    Swift ✅ ✅
    Objective-C ✅ ⏳
    Xcode integration ❌ ✅
    Multi platform support ❌ ✅
    Resources ✅

    View Slide

  73. SPM vs CocoaPods
    Swift ✅ ✅
    Objective-C ✅ ⏳
    Xcode integration ❌ ✅
    Multi platform support ❌ ✅
    Resources ✅ ⏳

    View Slide

  74. View Slide

  75. –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!”

    View Slide

  76. –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!”

    View Slide

  77. View Slide

  78. View Slide

  79. View Slide

  80. View Slide

  81. View Slide

  82. Takeaways

    View Slide

  83. Code Reuse Evolution
    ⭐ Static libs →

    View Slide

  84. Code Reuse Evolution
    ⭐ Static libs →
    ⭐⭐ Dynamic libs →

    View Slide

  85. Code Reuse Evolution
    ⭐ Static libs →
    ⭐⭐ Dynamic libs →
    ⭐⭐⭐ Frameworks, Modules →

    View Slide

  86. Code Reuse Evolution
    ⭐ Static libs →
    ⭐⭐ Dynamic libs →
    ⭐⭐⭐ Frameworks, Modules →
    ⭐⭐⭐⭐ Swift Packages

    View Slide

  87. Triggers: when separate code

    View Slide

  88. Triggers: when separate code
    1. You have files included in
    several targets

    View Slide

  89. 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

    View Slide

  90. What to put in a separate lib?
    •Networking logic

    View Slide

  91. What to put in a separate lib?
    •Networking logic
    •Foundation enhancements

    View Slide

  92. What to put in a separate lib?
    •Networking logic
    •Foundation enhancements
    •Trial limitations

    View Slide

  93. What to put in a separate lib?
    •Networking logic
    •Foundation enhancements
    •Trial limitations
    •Working with database

    View Slide

  94. What to put in a separate lib?
    •Networking logic
    •Foundation enhancements
    •Trial limitations
    •Working with database
    • Analytics

    View Slide

  95. What to put in a separate lib?
    •Networking logic
    •Foundation enhancements
    •Trial limitations
    •Working with database
    • Analytics
    •Every new feature

    View Slide

  96. It is to have
    a library per
    each feature

    View Slide

  97. It is to have
    a library per
    each feature
    Not 70

    View Slide

  98. 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:)

    View Slide

  99. Tips on designing an interface
    • Allow also simple default setup (have little or no
    parameters)
    init()

    View Slide

  100. Tips on designing an interface
    • Allow asynchronous execution
    func execute(param0:param1:completionHandler:)

    View Slide

  101. Tips on designing an interface
    • Use Apple Frameworks as example
    func tableView(_ tableView: UITableView,
    didDeselectRowAt indexPath: IndexPath)

    View Slide

  102. Tips on designing an interface
    • Use famous and popular libraries and frameworks as examples

    View Slide

  103. What to use
    if I start a project today?

    View Slide

  104. What to use
    if I start a project today?
    Frameworks

    View Slide

  105. What to use
    if I start a project today?
    Swift Package Manager

    View Slide

  106. Thank you!
    iaronskaya
    Q&A

    View Slide