LoginSignup
44
39

More than 5 years have passed since last update.

Swift2.3/Swift3でCocoaPodsを使い始めようとしてハマったことメモ

Posted at

そろそろSwift3移行やろうと思ったら開幕で一ハマりしたのでメモ。確認してないけどSwift2.3でも同様のはず。

ハマったこと。

コンパイルエラー。

“Use Legacy Swift Language Version” (SWIFT_VERSION) is required to be configured correctly for targets which use Swift. Use the [Edit > Convert > To Current Swift Syntax…] menu to choose a Swift version or use the Build Settings editor to configure the build setting directly.

結論。

CocoaPodsを1.1にあげてPodfileにswift_version= '3.0'と書く。

以下詳細。

経緯

  1. 既存のSwift2.2@Xcode7プロジェクトのSwift3移行を画策
  2. Xcode8で.xcworkspaceを開くとswiftのバージョンを2.3にするか3.0にするか聞かれるので3.0を選ぶ
  3. 自動migrateの対象を聞かれるので、Podsのターゲットは外してmigrate実行(注意:言うまでもなくバージョン管理必須
    • Build Settingsの"Use Legacy Swift Language Version"がNOになるのが分岐点
    • (.xcodeproj/project.pbxファイル中での書かれ方はSWIFT_VERSION=3.0)
  4. migrate後、Podsターゲットでエラーが発生しまくっているので、次にPodsをSwift3対応バージョンにアップデート
    • 単純に最新stableバージョンがSwift3対応していれば単純に最新バージョンに上げればよかったり
    • まだswift3対応版はbeta版で ~> 3.0.beta とか書いてあり
    • 別ブランチ状態で git: => '****', branch: => 'swift3'って書いたり。
  5. pod update後、ビルドしようとしたら

“Use Legacy Swift Language Version” (SWIFT_VERSION) is required to be configured correctly for targets which use Swift. Use the [Edit > Convert > To Current Swift Syntax…] menu to choose a Swift version or use the Build Settings editor to configure the build setting directly.

原因

自分の本体のプロジェクトで “Use Legacy Swift Language Version” (SWIFT_VERSION) をSwift3に設定したように、Podsターゲットでも設定を記述することを求められている。

対策

CocoaPodsを1.1にあげてPodfileにswift_version= '3.0'と書くと、Podsのプロジェクトを生成するときによしなに設定してくれるようになる。

Podfile
platform :ios, '9.0'
swift_version = '3.0'   <=== 追加
use_frameworks!

target 'PROJECT_NAME' do
  pod '***'
  pod '***'
...

10/6現在、まだ1.1.がRC2なので、--preが必要。

gem update cocoapods --pre
pod update

補足

このPRでサポートが追加された。感謝。

[Installer] Use user defined swift version by DanToml · Pull Request #5540 · CocoaPods/CocoaPods

CocoaPodsがサポートする以前の対策として

Podfile
post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['SWIFT_VERSION'] = '3.0'
    end
  end
End

と書く、という情報の方がよく検索に引っかかる気がするけど、(まだRCだけど)CocoaPods側でサポートされたのでそちらを使っていく方がスマート。

44
39
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
44
39