112
109

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 5 years have passed since last update.

SwiftでCocoaPodsを使い始めようとしてハマったことメモ

Last updated at Posted at 2015-02-10

やろうとしたこと

CocoaPods 3.6-pre

道のり

CocoaPods バージョンアップ

$ sudo gem install cocoapods --pre
$ pod --version
0.36.0.beta.2

特に問題なし

Podfile 生成

CocoaPodsの記事だと手書きで書き始めてる風なところ、一応コマンドで生成させる。ターゲットごとのセクションとかつけて生成されるのでそっちに合わせる。
sourceplatform の部分を修正

$ pod init
$ vi Podfile
source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '8.0'

target 'XXXXX' do
end

target 'XXXXXTests' do

end

Alamofire 追加

  pod 'Alamofire', :git => "https://github.com/Alamofire/Alamofire.git"

CocoaPodsの記事だと :git => "https://github.com/mrackwitz/Alamofire.git", :branch => "podspec" になっているところが新しいレポジトリに移動しているので変更。新レポジトリにはmasterに.podspecがあるのでブランチ指定は削除。

SwiftyJSON 追加

  pod 'SwiftyJSON', :git => "https://github.com/SwiftyJSON/SwiftyJSON.git"

ここも既にmasterブランチに.podspecが置かれているのでブランチ指定を削除。

Alamofire-SwiftyJSON 追加

  pod 'Alamofire-SwiftyJSON', :git => "https://github.com/SwiftyJSON/Alamofire-SwiftyJSON.git"

使おうとする

すべて追加し終わったので、いざ使おうとする。適当にAppDelegate.swiftあたりにネットワークアクセスを書いて動かしてみようとした。


        let parameters = [
            "xxxxx" : "xxxxx",
        ]
        Alamofire.request(.GET, "https://XXXXXXX", parameters: parameters)
            .responseSwiftyJSON { (request, response, json, error) in
                println(request)
                println(response)
                println(json)
                println(error)
        }

ハマったこと その1

が、ビルドできず。import Alamofire
Could not build objective-c module 'Alamofire'
と言われる。

しかも、Cleanしたりコード消してビルドしたりを繰り返していると、なんかビルドできて動くときもあって混乱。

ハマったこと その1 解決法

Alamofireモジュールがまだビルドできていないのが原因。こことかをみて気づけた。Alamofireだけ先にビルドしてやれば解決。

ただし自分の環境では、初期状態ではビルドターゲットにPodsのものが表示されていなかったので
Manage Schemes で Pods-[Target名]-Alamofire とかにチェックをつけて表示させる。

Alamofire、SwiftyJSON、Alamofire-SwiftyJSONを順にビルドしてやってから、自分のターゲットをビルドすると少し進んだ。

(ときどきビルドできて混乱させられたのは、Alamofireを呼び出さい状態で一度ビルド->モジュールが生成される->呼び出すコードを書く、としたときにたまたまうまく動いていたんだと思う)

ハマったこと その2

ちょっとしょぼい。Alamofire-SwiftyJSONがインポートできなくてしばらく困った。

ハマったこと その2 解決法

import Alamofire-SwiftyJSONimport AlamofireSwiftyJSONとかを試してうまく行かずなにかおかしいのかとしばらく調査。
結果、


import Alamofire_SwiftyJSON

が正解だったというただそれだけのことでした・・・。

勘で、WorcspaceのPods/Products/に表示される Alamofire_SwiftyJSON.framework のところの名前でimportすればいいっていうルールじゃないかと予想。

インストール成功

これでちゃんと動くようになりました。
以上、自分がCocoaPodsでSwiftライブラリをインストールするのにハマったところとその解決法でした。

ブログにも投下。

追記

Alamofire, SwiftyJSONはもうCocoaPods.orgにも出てきていて

  pod 'Alamofire'
  pod 'SwiftyJSON'
  pod 'Alamofire-SwiftyJSON', :git => "https://github.com/SwiftyJSON/Alamofire-SwiftyJSON.git"

でOKだった。

追記2 ハマったこと その3&解決法

CocoaPods 0.36.0.rc1から、上記のまま pod install すると

[!] Pods written in Swift can only be integrated as frameworks; this feature is still in beta. Add `use_frameworks!` to your Podfile or target to opt into using it.

と警告されてSwiftライブラリがインストールされないので、指示されている通り

use_frameworks!

をPodfileに明示する必要あり。CHANGELOGにありました。

112
109
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
112
109

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?