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!

Federico Zanetello

December 15, 2020
Tweet

More Decks by Federico Zanetello

Other Decks in Programming

Transcript

  1. 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, ...
  2. Why should we use one? โ€” modularization โ€” single-responsibility โ€”

    testability โ€” share between di๏ฌ€erent projects/platforms โ€” blessed by ๏ฃฟ โ€” fun โ€” easy to use โ€” ...much more
  3. Create a Swift Package 1. Open your project 2. File

    > New > Swi! Package... 3. Name it and add it to your project
  4. Add Package to your App 1. Open app Project Editor

    2. Add the package under Frameworks, Libraries, and Embedded Content
  5. The Package Structure โ”œโ”€โ”€ .gitignore โ”œโ”€โ”€ Package.swift โ”œโ”€โ”€ README.md โ”œโ”€โ”€

    Sources โ”‚ โ””โ”€โ”€ MyLibrary โ”‚ โ””โ”€โ”€ MyLibrary.swift โ””โ”€โ”€ Tests โ”œโ”€โ”€ MyLibraryTests โ”‚ โ”œโ”€โ”€ MyLibraryTests.swift โ”‚ โ””โ”€โ”€ XCTestManifests.swift โ””โ”€โ”€ LinuxMain.swift
  6. 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"]), ] )
  7. The Package Structure โ”œโ”€โ”€ .gitignore โ”œโ”€โ”€ Package.swift โ”œโ”€โ”€ README.md โ”œโ”€โ”€

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

    {} public var body: some View { Text("This view is totally cool") } }
  9. 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"]), ] )
  10. MyLibrary.swift import SwiftUI public struct MyCoolView: View { public init()

    {} public var body: some View { Text("This view is totally cool") } }
  11. 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") } } }
  12. The Package Structure โ”œโ”€โ”€ .gitignore โ”œโ”€โ”€ Package.swift โ”œโ”€โ”€ README.md โ”œโ”€โ”€

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

    public var body: some View { VStack { Image("coolImage", bundle: .module) Text("This view is totally cool") } } }
  14. 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) } } }
  15. 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"]), ] )
  16. 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
  17. 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) } } }
  18. 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"]), ] )
  19. 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
  20. 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
  21. Testing a package import XCTest @testable import MyLibrary final class

    MyLibraryTests: XCTestCase { func testExample() { XCTAssertEqual(..., "Hello, World!") } }
  22. Adding an External Package 1. File > Swi! Packages >

    Add Package Dependency 2. Insert dependency .git URL and follow wizard
  23. Adding an External Package import AStack import MyLibrary import SwiftUI

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

    struct ContentView: View { var body: some View { AHStack { MyCoolView() AnotherCoolView() } } }