Home > AI > IOS > Dependency Manager > Swift Package >

Swift Package

Swift packages are reusable components of Swift, Objective-C, Objective-C++, C, or C++ code. They can bundle resources, vend their code as binaries, or depend on other packages. Use Swift packages to bundle executable code, for example a script, as an executable product, or create a package to vend shareable code as a library product. Packages that vend a library product help promote modularity in your code, make it easy to share code with others, and enable other developers to add functionality to their apps.

With Xcode, you can create a new Swift package, add code, resource files, and binaries, build the Swift package, and run its unit tests.

Components

Package.swift

The package manifest must begin with the string // swift-tools-version:, followed by a version number such as // swift-tools-version:5.3.

// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "MyLibrary",
    platforms: [
        .macOS(.v10_14), .iOS(.v13), .tvOS(.v13)
    ],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "MyLibrary",
            targets: ["MyLibrary"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "MyLibrary",
            dependencies: []),
        .testTarget(
            name: "MyLibraryTests",
            dependencies: ["MyLibrary"]),
    ]
)

Steps:

Step 1: create a local package

Step 2: publish the pacakge

Step 3: introduce the package in the project

Step 4: modify the package

Official link: (WWDC2019)https://developer.apple.com/videos/play/wwdc2019/410/

Related posts:
    No posts found.

Leave a Reply