7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Swift】マルチモジュール構成を試してみた

Posted at

はじめに

最近、マルチモジュール構成のやり方を知ったので記録しておきます。

Packageを作成する

① 「File」を選択します
② 「New」を選択します
③ 「Package...」を選択します
スクリーンショット 2023-04-11 23.19.37.png

④ パッケージ名を入力します(今回はCoreにしました)
スクリーンショット 2023-04-11 23.22.12.png

⑤ 保存したいディレクトリを選択します
スクリーンショット 2023-04-11 23.24.21.png

⑥ プロジェクトを選択します
⑦ 「Create」を選択します
スクリーンショット 2023-04-11 23.25.29.png

このようになりました。
スクリーンショット 2023-04-11 23.28.25.png

モジュールを作成する

今回はサンプルとしてAPIClientRepositoryのモジュールを作成してみようと思います。

CoreCoreTestsを選択します(コマンド押しながら選択すると複数選択できる)
② 右クリックをして「Delete」を選択します
スクリーンショット 2023-04-11 23.30.05.png

③ 「Move to Trash」を選択します
スクリーンショット 2023-04-11 23.31.31.png

④ 「Sources」を右クリックします
⑤ 「New Folder」を選択します
スクリーンショット 2023-04-11 23.34.18.png

APIClientと入力します
スクリーンショット 2023-04-11 23.36.05.png

上記の手順であと3つほどフォルダを作成します。

  • APIClient
  • Repository
  • APIClientTests
  • RepositoryTests

スクリーンショット 2023-04-11 23.38.28.png

フォルダ内にファイルがないとエラーになるので適当なファイルを作成します。
スクリーンショット 2023-04-11 23.43.00.png

Package.swiftの編集

Package.swift
import PackageDescription

let package = Package(
    name: "Core",
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "APIClient",
            targets: ["APIClient"]
        ),
        .library(
            name: "Repository",
            targets: ["Repository"]
        ),
    ],
    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: "APIClient",
            dependencies: []
        ),
        .target(
            name: "Repository",
            dependencies: []
        ),
        .testTarget(
            name: "APIClientTests",
            dependencies: ["APIClient"]
        ),
        .testTarget(
            name: "RepositoryTests",
            dependencies: ["Repository"]
        ),
    ]
)

使う

このようにインポートできるようになリます。
スクリーンショット 2023-04-11 23.44.47.png

おわり

さっそく個人開発アプリに導入してみました

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?