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. 3.
  2. 4.
  3. 5.

    @aronskaya • macOS Chapter Lead at Women Who Code Kyiv

    • workshop mentor at SwiftAveiro • one of founders of MacPaw Tech Talks
  4. 7.
  5. 10.

    Behid this talk 1⃣ projects with 1 target apps, containing

    several apps C++ libraries, shared with Linux
  6. 11.

    Behid this talk 1⃣ projects with 1 target apps, containing

    several apps C++ libraries, shared with Linux projects with dozens internal libs
  7. 15.

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

    dylibs, frameworks, modules Dependency management: CocoaPods, Swift Package Manager
  8. 16.

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

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

    Why

  10. 18.
  11. 19.
  12. 20.
  13. 34.

    Programmer evolution it works! it works & is grouped in

    classes it works & is grouped in reusable classes
  14. 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
  15. 42.
  16. 46.
  17. 47.
  18. 48.
  19. 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
  20. 50.
  21. 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
  22. 52.
  23. 53.

    Recap Static library headers + code, embedded into client's executable

    Dynamic library headers + code in a separate file Framework dynamic library + resources
  24. 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.”
  25. 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.”
  26. 62.

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

    •module.modulemap •Swift only •.swiftdoc, .swiftmodule
  27. 67.

    SPM vs CocoaPods Swift ✅ ✅ Objective-C ✅ ⏳ Xcode

    integration Multi platform support Resources
  28. 68.

    SPM vs CocoaPods Swift ✅ ✅ Objective-C ✅ ⏳ Xcode

    integration ❌ Multi platform support Resources
  29. 69.

    SPM vs CocoaPods Swift ✅ ✅ Objective-C ✅ ⏳ Xcode

    integration ❌ ✅ Multi platform support Resources
  30. 70.

    SPM vs CocoaPods Swift ✅ ✅ Objective-C ✅ ⏳ Xcode

    integration ❌ ✅ Multi platform support ❌ Resources
  31. 71.

    SPM vs CocoaPods Swift ✅ ✅ Objective-C ✅ ⏳ Xcode

    integration ❌ ✅ Multi platform support ❌ ✅ Resources
  32. 72.

    SPM vs CocoaPods Swift ✅ ✅ Objective-C ✅ ⏳ Xcode

    integration ❌ ✅ Multi platform support ❌ ✅ Resources ✅
  33. 73.

    SPM vs CocoaPods Swift ✅ ✅ Objective-C ✅ ⏳ Xcode

    integration ❌ ✅ Multi platform support ❌ ✅ Resources ✅ ⏳
  34. 74.
  35. 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!”
  36. 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!”
  37. 77.
  38. 78.
  39. 79.
  40. 80.
  41. 81.
  42. 82.
  43. 85.

    Code Reuse Evolution ⭐ Static libs → ⭐⭐ Dynamic libs

    → ⭐⭐⭐ Frameworks, Modules →
  44. 86.

    Code Reuse Evolution ⭐ Static libs → ⭐⭐ Dynamic libs

    → ⭐⭐⭐ Frameworks, Modules → ⭐⭐⭐⭐ Swift Packages
  45. 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
  46. 93.

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

    enhancements •Trial limitations •Working with database
  47. 94.

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

    enhancements •Trial limitations •Working with database • Analytics
  48. 95.

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

    enhancements •Trial limitations •Working with database • Analytics •Every new feature
  49. 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:)
  50. 99.

    Tips on designing an interface • Allow also simple default

    setup (have little or no parameters) init()
  51. 100.
  52. 101.

    Tips on designing an interface • Use Apple Frameworks as

    example func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)
  53. 102.

    Tips on designing an interface • Use famous and popular

    libraries and frameworks as examples