注意
CarthageでRealmをswiftで使う場合はSwiftは2.1以上にしてください。
Swift2.0ですとエラーになります。
Swift2.1では問題なく導入可能です。
手順
手順1 Carthage導入するために「Homebrew」を導入する
Carthageを使うにはまず「Homebrew(Mac OSX用のパッケージマネージャ)」が必要になりますので、まず「Homebrew」があるか、バグや予期せぬエラーに備え古すぎるバージョンになっていないかを確認します。
(Homebrewという名前ですが使用するコマンドは「brew」になります。)
コマンド
brew --version
結果
g08m11:$ brew --version
Homebrew 0.9.5 (git revision 5cbcb; last commit 2015-12-10)
Homebrewがない人は
コマンド
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew update
Homebrewがあるがバージョンが古すぎる人は
コマンド
brew update
を実行してください。
手順2 carthageの導入
Homebrew経由でcarthageを導入します。
コマンド
brew install carthage
結果
g08m11:MyApps $ brew install carthage
==> Downloading https://homebrew.bintray.com/bottles/carthage-0.11.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring carthage-0.11.yosemite.bottle.tar.gz
🍺 /usr/local/Cellar/carthage/0.11: 29 files, 11M
carthage導入インジケータが100%になれば導入完了です。
手順3 手順1で作った「todoSample」があるディレクトリまで移動
(私は「work/swift/MyApps」というディレクトリ配下に「todoSample」を作りました。)
コマンド
g08m11:$ cd ~/work/swift/MyApps/todoSample/
手順4 vimなどでCartfileの作成および編集で「Realm+ SwftyJson + Alamofire」を外部ライブラリとして指定する
*コマンド
vi Cartfile
開いたら「i」キーを押下して入力モードにし、
以下の内容を記述する
記述内容
github "SwiftyJSON/SwiftyJSON"
github "Alamofire/Alamofire"
github "realm/realm-cocoa"
入力後は「esc」キーを押下して編集モードに戻り「:wq」で上書き保存 + 強制終了をします。
手順4 Cartfileに記述した外部ライブラリをビルドする
carthage updateに加えて今回はiOS向けのアプリを対象としているので、「--platform iOS」をオプション指定します。
また今回はビルドだけでどれだけ時間が掛かるのか「time」コマンドも使い確認していきたいと思います。
コマンド
time carthage update --platform iOS
結果
g08m11:todoSample $ time carthage update --platform iOS
*** Fetching realm-cocoa
*** Cloning SwiftyJSON
*** Cloning Alamofire
*** Downloading realm-cocoa.framework binary at "v0.96.3"
*** Checking out Alamofire at "3.1.3"
*** Checking out SwiftyJSON at "2.3.3"
*** xcodebuild output can be found in /var/folders/3r/x615hkv93vg706qrjsss_bxm0000gn/T/carthage-xcodebuild.yl9xcu.log
*** Building scheme "Alamofire iOS" in Alamofire.xcworkspace
2015-12-12 13:14:01.039 xcodebuild[27364:1507012] [MT] PluginLoading: Required plug-in compatibility UUID 0420B86A-AA43-4792-9ED0-6FE0F2B16A13 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/InjectionPlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2015-12-12 13:15:34.764 xcodebuild[27600:1507996] [MT] PluginLoading: Required plug-in compatibility UUID 0420B86A-AA43-4792-9ED0-6FE0F2B16A13 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/InjectionPlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
*** Building scheme "SwiftyJSON iOS" in SwiftyJSON.xcworkspace
2015-12-12 13:17:03.349 xcodebuild[27750:1508628] [MT] PluginLoading: Required plug-in compatibility UUID 0420B86A-AA43-4792-9ED0-6FE0F2B16A13 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/InjectionPlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2015-12-12 13:17:21.131 xcodebuild[27801:1508818] [MT] PluginLoading: Required plug-in compatibility UUID 0420B86A-AA43-4792-9ED0-6FE0F2B16A13 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/InjectionPlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
/Users/g08m11/work/swift/MyApps/todoSample/Carthage/Checkouts/Alamofire/Tests/NSURLSessionConfiguration+AlamofireTests.swift:32:55: warning: 'backgroundSessionConfiguration' was deprecated in iOS 8.0: Please use backgroundSessionConfigurationWithIdentifier: instead
/Users/g08m11/work/swift/MyApps/todoSample/Carthage/Checkouts/Alamofire/Tests/NSURLSessionConfiguration+AlamofireTests.swift:32:55: warning: 'backgroundSessionConfiguration' was deprecated in iOS 8.0: Please use backgroundSessionConfigurationWithIdentifier: instead
/Users/g08m11/work/swift/MyApps/todoSample/Carthage/Checkouts/Alamofire/Tests/NSURLSessionConfiguration+AlamofireTests.swift:32:55: warning: 'backgroundSessionConfiguration' was deprecated in iOS 8.0: Please use backgroundSessionConfigurationWithIdentifier: instead
/Users/g08m11/work/swift/MyApps/todoSample/Carthage/Checkouts/Alamofire/Tests/NSURLSessionConfiguration+AlamofireTests.swift:32:55: warning: 'backgroundSessionConfiguration' was deprecated in iOS 8.0: Please use backgroundSessionConfigurationWithIdentifier: instead
real 5m11.360s
user 9m39.174s
sys 0m52.100s
g08m11:todoSample $
実行時間抜粋
real 5m11.360s
user 9m39.174s
sys 0m52.100s
g08m11:todoSample $
とってはみたものの早いかどうかはわからなかったので今度、別記事であげてみたいと思います。
手順5 プロジェクトに外部ライブラリを追加
まずプロジェクトファイルを開き「①エディタエリア内の『TARGETS』」が選択されていることを確認し、
「②Generalタブ」を選択、「③Linked Frameworks and Library」にある「④+ボタン」を押します。
Frameworksを選択する画面になるので、「Add Other...」を選択します。
プロジェクトのディレクトリ内から、Carthage/Build/iOSと移動し、
ライブラリの.frameworkファイルを選択します。
追加しましたら「選択した外部ライブラリ」が変更になっていることが確認出来るかと思います。
ライブラリが追加できたら、「①Build Phasesタブ」に移動し、
「②+ボタン」から、「③New Run Script Phase」を選択します。
選択後、「Run Script」が追加されていることが確認出来るかと思います。
ここでまず「Shell」の下にある「枠」の部分にCarthageを使ってインストールしたフレームワークすべてに必要な処理として以下のコマンドを記述します。
(こちらはクリックしますと入力可能となります。)
/usr/local/bin/carthage copy-frameworks
そして次に「input Files」にて+ボタンを押し、追加したframework(SwiftyJSON、Alamofire、Realm、RealmSwift)の情報を記述します。
$(SRCROOT)/Carthage/Build/iOS/SwiftyJSON.framework
$(SRCROOT)/Carthage/Build/iOS/Alamofire.framework
$(SRCROOT)/Carthage/Build/iOS/Realm.framework
$(SRCROOT)/Carthage/Build/iOS/RealmSwift.framework
追加後はバグが無いかなどの確認のためXcodeのクリーン(commadキー + shiftキー + K)と
ビルド(commandキー + B)をします。
(「$(SRCROOT)/Carthage/Build/iOS/SwiftyJSON.framework」は末尾に空白などが入ってもエラーになるため記述ミスには注意しましょう。)
ここまでやったのですがSwift2.0だったため、エラーとなりました。。。
carthageもRealmも大変素晴らしいですがSwiftやXcodeのバージョンによってはエラーなどの原因となるため、今後は気をつけたいと思います。
(ちなみに、RealmのサイトにはSwift2.0はchartage対象外ということが分かります。)