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

Swift Package all the things! ๐Ÿ“ฆ

Swift Package all the things!ย ๐Ÿ“ฆ

Apple has blessed SPM as the official way to manage dependencies in Xcode:
come and learn how easy it is to setup and use Swift Packages in your projects!

With this year support of assets and binaries, thereโ€™s never been a better time to adopt Swift Packages!

E35f18d9e5584b48a7cf5550f905522f?s=128

Federico Zanetello

December 15, 2020
Tweet

More Decks by Federico Zanetello

Other Decks in Programming

Transcript

  1. Swift Package all the things! Federico Zanetello ฬฃฬฃฬฃฬฃฬฃ ๏ฌvestars.blog โ€ข

    @zntfdr
  2. ๏ฌvestars.blog WWDCNotes.com Bangkok Metro

  3. What's a Swift Package? โ€” New way to build frameworks

    โ€” Swi! project since 2015 โ€” Xcode 11 โค iOS Apps โ€” Swi!, Objective-C, Objective-C++, C, or C++ โ€” Can bundle code, binaries, assets, ...
  4. Why should we use one? โ€” modularization โ€” single-responsibility โ€”

    testability โ€” share between di๏ฌ€erent projects/platforms โ€” blessed by ๏ฃฟ โ€” fun โ€” easy to use โ€” ...much more
  5. Getting Started

  6. MyApp

  7. Create a Swift Package 1. Open your project 2. File

    > New > Swi! Package... 3. Name it and add it to your project
  8. Create a Swift Package (1/2)

  9. Create a Swift Package (2/2)

  10. Done!

  11. Add Package to your App 1. Open app Project Editor

    2. Add the package under Frameworks, Libraries, and Embedded Content
  12. Add Package to your App (1/2)

  13. Add Package to your App (2/2)

  14. Done!

  15. The Package Structure โ”œโ”€โ”€ .gitignore โ”œโ”€โ”€ Package.swift โ”œโ”€โ”€ README.md โ”œโ”€โ”€

    Sources โ”‚ โ””โ”€โ”€ MyLibrary โ”‚ โ””โ”€โ”€ MyLibrary.swift โ””โ”€โ”€ Tests โ”œโ”€โ”€ MyLibraryTests โ”‚ โ”œโ”€โ”€ MyLibraryTests.swift โ”‚ โ””โ”€โ”€ XCTestManifests.swift โ””โ”€โ”€ LinuxMain.swift
  16. Package.swift // swift-tools-version:5.3 import PackageDescription let package = Package( name:

    "MyLibrary", products: [ .library(name: "MyLibrary", targets: ["MyLibrary"]), ], dependencies: [], targets: [ .target(name: "MyLibrary", dependencies: []), .testTarget(name: "MyLibraryTests", dependencies: ["MyLibrary"]), ] )
  17. The Package Structure โ”œโ”€โ”€ .gitignore โ”œโ”€โ”€ Package.swift โ”œโ”€โ”€ README.md โ”œโ”€โ”€

    Sources โ”‚ โ””โ”€โ”€ MyLibrary โ”‚ โ””โ”€โ”€ MyLibrary.swift โ””โ”€โ”€ Tests โ”œโ”€โ”€ MyLibraryTests โ”‚ โ”œโ”€โ”€ MyLibraryTests.swift โ”‚ โ””โ”€โ”€ XCTestManifests.swift โ””โ”€โ”€ LinuxMain.swift
  18. MyLibrary.swift struct MyLibrary { var text = "Hello, World!" }

  19. MyLibrary.swift import SwiftUI public struct MyCoolView: View { public init()

    {} public var body: some View { Text("This view is totally cool") } }
  20. Package.swift // swift-tools-version:5.3 import PackageDescription let package = Package( name:

    "MyLibrary", platforms: [.iOS(.v13)], products: [ .library(name: "MyLibrary", targets: ["MyLibrary"]), ], dependencies: [], targets: [ .target(name: "MyLibrary", dependencies: []), .testTarget(name: "MyLibraryTests", dependencies: ["MyLibrary"]), ] )
  21. MyLibrary.swift import SwiftUI public struct MyCoolView: View { public init()

    {} public var body: some View { Text("This view is totally cool") } }
  22. Add images import SwiftUI public struct MyCoolView: View { public

    init() {} public var body: some View { VStack { Image("coolImage", bundle: .module) Text("This view is totally cool") } } }
  23. The Package Structure โ”œโ”€โ”€ .gitignore โ”œโ”€โ”€ Package.swift โ”œโ”€โ”€ README.md โ”œโ”€โ”€

    Sources โ”‚ โ””โ”€โ”€ MyLibrary โ”‚ โ”œโ”€โ”€ Assets.xcassets โ”‚ โ””โ”€โ”€ MyLibrary.swift โ””โ”€โ”€ Tests โ”œโ”€โ”€ MyLibraryTests โ”‚ โ”œโ”€โ”€ MyLibraryTests.swift โ”‚ โ””โ”€โ”€ XCTestManifests.swift โ””โ”€โ”€ LinuxMain.swift
  24. import SwiftUI public struct MyCoolView: View { public init() {}

    public var body: some View { VStack { Image("coolImage", bundle: .module) Text("This view is totally cool") } } }
  25. Add localization import SwiftUI public struct MyCoolView: View { public

    init() {} public var body: some View { VStack { Image("coolImage", bundle: .module) Text("cool_view_text", bundle: .module) } } }
  26. Package.swift // swift-tools-version:5.3 import PackageDescription let package = Package( name:

    "MyLibrary", defaultLocalization: LanguageTag("en"), platforms: [.iOS(.v13)], products: [ .library(name: "MyLibrary", targets: ["MyLibrary"]), ], dependencies: [], targets: [ .target(name: "MyLibrary", dependencies: []), .testTarget(name: "MyLibraryTests", dependencies: ["MyLibrary"]), ] )
  27. The Package Structure โ”œโ”€โ”€ .gitignore โ”œโ”€โ”€ Package.swift โ”œโ”€โ”€ README.md โ”œโ”€โ”€

    Sources โ”‚ โ””โ”€โ”€ MyLibrary โ”‚ โ”œโ”€โ”€ Assets.xcassets โ”‚ โ”œโ”€โ”€ Localizations // ! Arbitrary name โ”‚ โ”‚ โ”œโ”€โ”€ en.lproj โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Localizable.strings โ”‚ โ”‚ โ””โ”€โ”€ th.lproj โ”‚ โ”‚ โ””โ”€โ”€ Localizable.strings โ”‚ โ””โ”€โ”€ MyLibrary.swift โ””โ”€โ”€ Tests โ”œโ”€โ”€ MyLibraryTests โ”‚ โ”œโ”€โ”€ MyLibraryTests.swift โ”‚ โ””โ”€โ”€ XCTestManifests.swift โ””โ”€โ”€ LinuxMain.swift
  28. Add localization import SwiftUI public struct MyCoolView: View { public

    init() {} public var body: some View { VStack { Image("coolImage", bundle: .module) Text("cool_view_text", bundle: .module) } } }
  29. Testing our package

  30. Testing a package // swift-tools-version:5.3 import PackageDescription let package =

    Package( name: "MyLibrary", defaultLocalization: LanguageTag("en"), platforms: [.iOS(.v13)], products: [ .library(name: "MyLibrary", targets: ["MyLibrary"]), ], dependencies: [], targets: [ .target(name: "MyLibrary", dependencies: []), .testTarget(name: "MyLibraryTests", dependencies: ["MyLibrary"]), ] )
  31. Testing a package โ”œโ”€โ”€ .gitignore โ”œโ”€โ”€ Package.swift โ”œโ”€โ”€ README.md โ”œโ”€โ”€

    Sources โ”‚ โ””โ”€โ”€ MyLibrary โ”‚ โ”œโ”€โ”€ Assets.xcassets โ”‚ โ”œโ”€โ”€ Localizations โ”‚ โ”‚ โ”œโ”€โ”€ en.lproj โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Localizable.strings โ”‚ โ”‚ โ””โ”€โ”€ th.lproj โ”‚ โ”‚ โ””โ”€โ”€ Localizable.strings โ”‚ โ””โ”€โ”€ MyLibrary.swift โ””โ”€โ”€ Tests โ”œโ”€โ”€ MyLibraryTests โ”‚ โ”œโ”€โ”€ MyLibraryTests.swift โ”‚ โ””โ”€โ”€ XCTestManifests.swift โ””โ”€โ”€ LinuxMain.swift
  32. Testing a package โ”œโ”€โ”€ .gitignore โ”œโ”€โ”€ Package.swift โ”œโ”€โ”€ README.md โ”œโ”€โ”€

    Sources โ”‚ โ””โ”€โ”€ MyLibrary โ”‚ โ”œโ”€โ”€ Assets.xcassets โ”‚ โ”œโ”€โ”€ Localizations โ”‚ โ”‚ โ”œโ”€โ”€ en.lproj โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Localizable.strings โ”‚ โ”‚ โ””โ”€โ”€ th.lproj โ”‚ โ”‚ โ””โ”€โ”€ Localizable.strings โ”‚ โ””โ”€โ”€ MyLibrary.swift โ””โ”€โ”€ Tests โ””โ”€โ”€ MyLibraryTests โ””โ”€โ”€ MyLibraryTests.swift
  33. Testing a package import XCTest @testable import MyLibrary final class

    MyLibraryTests: XCTestCase { func testExample() { XCTAssertEqual(..., "Hello, World!") } }
  34. Using our package

  35. Using our package import MyLibrary import SwiftUI struct ContentView: View

    { var body: some View { MyCoolView() } }
  36. Done! ! import MyLibrary import SwiftUI struct ContentView: View {

    var body: some View { MyCoolView() } }
  37. External packages

  38. Adding an External Package 1. File > Swi! Packages >

    Add Package Dependency 2. Insert dependency .git URL and follow wizard
  39. Adding an External Package (1/2)

  40. Adding an External Package (2/2)

  41. Adding an External Package (2/2)

  42. Adding an External Package (2/2)

  43. Done!

  44. Adding an External Package import AStack import MyLibrary import SwiftUI

    struct ContentView: View { var body: some View { AHStack { MyCoolView() AnotherCoolView() } } }
  45. Adding an External Package import AStack import MyLibrary import SwiftUI

    struct ContentView: View { var body: some View { AHStack { MyCoolView() AnotherCoolView() } } }
  46. Swift Package all the things! Federico Zanetello ฬฃฬฃฬฃฬฃฬฃ ๏ฌvestars.blog โ€ข

    @zntfdr