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. @aronskaya • macOS Chapter Lead at Women Who Code Kyiv

    • workshop mentor at SwiftAveiro • one of founders of MacPaw Tech Talks
  2. Behid this talk 1⃣ projects with 1 target apps, containing

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

    several apps C++ libraries, shared with Linux projects with dozens internal libs
  4. Agenda ⁉ Why separate codebase How to do it: static,

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

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

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

    classes it works & is grouped in reusable classes
  8. Programmer evolution it works! it works & is grouped in

    classes it works & is grouped in reusable classes it works & grouped in a reusable module
  9. 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
  10. 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
  11. Recap Static library headers + code, embedded into client's executable

    Dynamic library headers + code in a separate file Framework dynamic library + resources
  12. –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.”
  13. –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.”
  14. 2 Types of Modules Clang module Swift module •Objective-C, Swift

    •module.modulemap •Swift only •.swiftdoc, .swiftmodule
  15. SPM vs CocoaPods Swift ✅ ✅ Objective-C ✅ ⏳ Xcode

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

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

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

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

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

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

    integration ❌ ✅ Multi platform support ❌ ✅ Resources ✅ ⏳
  22. –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!”
  23. –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!”
  24. Code Reuse Evolution ⭐ Static libs → ⭐⭐ Dynamic libs

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

    → ⭐⭐⭐ Frameworks, Modules → ⭐⭐⭐⭐ Swift Packages
  26. 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
  27. What to put in a separate lib? •Networking logic •Foundation

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

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

    enhancements •Trial limitations •Working with database • Analytics •Every new feature
  30. 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:)
  31. Tips on designing an interface • Allow also simple default

    setup (have little or no parameters) init()
  32. Tips on designing an interface • Use Apple Frameworks as

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

    libraries and frameworks as examples