Pro Yearly is on sale from $80 to $50! »

Swift Code Reuse Made Simple

6e39a16132f9e41e6869527ba0ced276?s=47 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.

6e39a16132f9e41e6869527ba0ced276?s=128

vashchenko

November 15, 2019
Tweet

Transcript

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

  2. @aronskaya • macOS Engineer at MacPaw • CleanMyMac

  3. Hoover

  4. Fixel

  5. @aronskaya • macOS Chapter Lead at Women Who Code Kyiv

    • workshop mentor at SwiftAveiro • one of founders of MacPaw Tech Talks
  6. Swift code reuse made simple modules, packages, libraries

  7. None
  8. Behind this talk 1⃣ projects with 1 target

  9. Behind this talk 1⃣ projects with 1 target apps, containing

    several apps
  10. Behid this talk 1⃣ projects with 1 target apps, containing

    several apps C++ libraries, shared with Linux
  11. Behid this talk 1⃣ projects with 1 target apps, containing

    several apps C++ libraries, shared with Linux projects with dozens internal libs
  12. 1 app == 27 projects up to 14 targets per

    project
  13. Agenda ⁉ Why separate codebase

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

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

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

    dylibs, frameworks, modules Takeaways: best practices & modern approach Dependency management: CocoaPods, Swift Package Manager
  17. Why

  18. None
  19. None
  20. None
  21. Faster compilation

  22. Faster app launch

  23. Smaller app bundle

  24. ⌚ Easily support new platforms

  25. Grow your GitHub

  26. Test once use again and again

  27. Learn&Train to think bigger

  28. How Apple does it

  29. How Apple does it

  30. How Apple does it 172

  31. Programmer evolution

  32. Programmer evolution it works!

  33. Programmer evolution it works! it works & is grouped in

    classes
  34. Programmer evolution it works! it works & is grouped in

    classes it works & is grouped in reusable classes
  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
  36. The Versus Quiz

  37. Static Dynamic

  38. Static linking / libraries

  39. Static library

  40. Amazing Notes app

  41. Amazing Notes app

  42. None
  43. Dynamic linking / libraries

  44. Dynamic library dylib

  45. Library Framework

  46. None
  47. None
  48. None
  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
  50. dylib

  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
  52. None
  53. Recap Static library headers + code, embedded into client's executable

    Dynamic library headers + code in a separate file Framework dynamic library + resources
  54. Library Module

  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.”
  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.”
  57. Module is just another type of header

  58. ❌Implementation

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

    library
  60. 2 Types of Modules

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

  62. 2 Types of Modules Clang module Swift module •Objective-C, Swift

    •module.modulemap •Swift only •.swiftdoc, .swiftmodule
  63. Swift Package Manager CocoaPods

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

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

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

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

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

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

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

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

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

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

    integration ❌ ✅ Multi platform support ❌ ✅ Resources ✅ ⏳
  74. None
  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!”
  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!”
  77. None
  78. None
  79. None
  80. None
  81. None
  82. Takeaways

  83. Code Reuse Evolution ⭐ Static libs →

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

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

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

    → ⭐⭐⭐ Frameworks, Modules → ⭐⭐⭐⭐ Swift Packages
  87. Triggers: when separate code

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

    several targets
  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
  90. What to put in a separate lib? •Networking logic

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

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

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

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

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

    enhancements •Trial limitations •Working with database • Analytics •Every new feature
  96. It is to have a library per each feature

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

    70
  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:)
  99. Tips on designing an interface • Allow also simple default

    setup (have little or no parameters) init()
  100. Tips on designing an interface • Allow asynchronous execution func

    execute(param0:param1:completionHandler:)
  101. Tips on designing an interface • Use Apple Frameworks as

    example func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)
  102. Tips on designing an interface • Use famous and popular

    libraries and frameworks as examples
  103. What to use if I start a project today?

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

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

    Package Manager
  106. Thank you! iaronskaya Q&A