LoginSignup
4
2

More than 5 years have passed since last update.

iOSアプリにReflectionライブラリ導入、あるいはSwift Package Manager製のライブラリをiOSアプリに入れる方法

Posted at

概要

  • ある日、structのオブジェクトに対して、文字列リテラルのプロパティ名を使って値をget/setしたくなりました。(Key-Value Coding)
  • どうやら Reflection というライブラリが
// Retrieves the value of `person.firstName`
let firstName: String = try get("firstName", from: person)

// Sets the value of `person.age`
try set(36, key: "age", for: &person)

こんな感じでget/set出来るので便利そう。

  • しかしReflectionはSwift Package Manager製・・・
    • Swift Package Managerは CocoaPodsCarthageと同様にライブラリの依存関係を管理してくれるツールですが現時点ではiOS未サポートとのこと・・
  • でも、iOSに組み込むことも出来て、swift-package-manager-ios にデモがあったのでそれを試してみた・・・のがこの記事になります。

環境

  • Swift4.1
  • Xcode9.3

手順

以下ではライブラリを入れたいXcodeProjectをTargetProjectと書きます。

1. 下準備

sudo gem install xcodeproj

cd path/to/適当な場所
git clone https://github.com/j-channings/swift-package-manager-ios.git
cd swift-package-manager-ios

# デモに入っているscript等を流用したい
cp generate-dependencies-project.rb path/to/TargetProject
cp Package.swift path/to/TargetProject # Swift Package Manager用の依存関係を記述するファイル
cp -r .deps-sources path/to/TargetProject

2. path/to/TargetProject/Package.swift に導入したいライブラリの情報を書く

今回はReflectionを導入したく、以下のようなファイルにしました。

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

import PackageDescription

let package = Package(
    name: "Dependencies",
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
        .library(
            name: "Dependencies",
            type: .static,
            targets: ["Dependencies"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/Zewo/Reflection.git", from: "0.16.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 which this package depends on.
        .target(
            name: "Dependencies",
            dependencies: ["Reflection"],
            path: ".deps-sources"
        )
    ]
)

3. 導入したいライブラリを含むxcodeprojを生成

cd path/to/TargetProject
ruby generate-dependencies-project.rb # => Dependencies.xcodeproj/ が生成される

4. Dependencies.xcodeproj をサブプロジェクトとして追加する

  1. TargetProjectをXcodeで開いて、左端のナビゲーターエリアにDependencies.xcodeprojeをドロップ

  2. General -> Linked Frameworks and Libraries の + を押して 「Reflection.framework」を追加

  3. import Reflection を入れた上でビルドが通るはず!

以上です :innocent:

Special Thanks: @ask-a-lie

4
2
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
4
2