LoginSignup
27
21

More than 5 years have passed since last update.

Playground で Carthage ライブラリを import する

Posted at

View を作るのに毎回ビルドするのは時間がかかるので Playground でやりたい。
ググると幾つか手順が出てくるが、私のやり方が悪いのかうまくいかなかったので、2017年3月21日時点でうまく行ったやり方を書いておく。

やり方

まず Project を作成する

Xcode の File > New > Project で新規作成。

xcworkspace の作成

File> Save as Workspace をすると、 xcworkspace がつくられる。
xcode を一回閉じて、 今作った xcworkspace を開く。

Carthage ライブラリのインストール

今回は SnapKit と Rx を入れる。

$ vi Cartfile
github "SnapKit/SnapKit"
github "ReactiveX/RxSwift"

$ carthage update --platform iOS --use-submodules

ライブラリの xcodeproj を Project に import

Project/Carthage/Checkouts/SnapKit/SnapKit.xcodeproj, Project/Carthage/Checkouts/RxSwift/Rx.xcodeproj をドラッグアンドドロップで Xcode にコピー。

こうなる。

unspecified.png

Embedded Libraries にコピー

Project/Carthage/Build/iOS/ に framework があるので、 RxSwift, RxCocoa, SnapKit など必要なのをドラッグアンドドロップで Embedded Libraries に入れる。

こうなる。

unspecified.png

Build する

⌘ + B で一旦 build しておく (しなくてもいけるかも)。

Playground ファイルにコードを書く

New File で Playground ファイルを作成する。
Targets などはチェックをつけないで大丈夫。

ファイルを作ったらとりあえず以下のように書いてみる。

// SnapKit + Rx で画面を作成する

import UIKit
import SnapKit
import RxSwift
import RxCocoa
import PlaygroundSupport

class VC: UIViewController {
    let button: UIButton = {
        let button = UIButton()
        button.setTitle("button", for: .normal)
        return button
    }()

    let disposeBag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        view.addSubview(button)
        button.backgroundColor = UIColor.red
        button.snp.makeConstraints{ make in
            make.width.height.equalTo(100)
            make.top.left.equalTo(100)
        }

        button.rx.tap
            .subscribe(onNext: { _ in
                print("tap")
            })
            .disposed(by: disposeBag)
    }
}

let vc = VC()
vc.title = "title"
let nav = UINavigationController(rootViewController: vc)
nav.view.frame = CGRect(x: 0, y: 0, width: 320, height: 568)

/// MARK: - View を表示

PlaygroundPage.current.liveView = nav
PlaygroundPage.current.needsIndefiniteExecution = true

ちょっと待つと

SnapKit, Rx を使い画面を Playground で作れるようになりました :tada:

unspecified.png

サンプルリポジトリ

starhoshi/PlaygroundWithCarthage を clone すれば試せます。

$ git clone git@github.com:starhoshi/PlaygroundWithCarthage.git
$ cd PlaygroundWithCarthage
$ open PlaygroundWithCarthage.xcworkspace

これで Playground file を開けばすぐ試せます :ok_woman:

参考

Swift PlaygroundでCocoaPodsのライブラリを使う - Qiita
【iOS】Carthageで入れたframeworkをPlaygroundで使用する - Qiita

27
21
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
27
21