LoginSignup
5
3

More than 5 years have passed since last update.

swift2 から swift3 へ移行したときに realm が使えなくなったときの解決法

Posted at

状況

今回xcodeのアップデートにともなってswift2がlegacyコードになったわけですが、
その際にpodで管理していたrealmが使えなくなってしまいました。
今回解決したので共有します。

まずやること

公式ドキュメントを読むのって大事ですね
https://realm.io/jp/docs/swift/latest/

Xcode 8を利用している場合、下記のコードをPodfileに追加してください。Swiftバージョンは自分の環境に合わせて適切に変更してください。

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

今回swift2系からの移行になるので当然この設定がされていませんでした。

状況次第ではこれで解決するかもしれません。

そのあと出たエラーと解決法

no such module realmswift

まずビルドスキームを追加します。
自分プロジェクトのスキーム→Manage Schemesを押すとRealm とRealmSwiftがあるので、
それをチェックします。

そのあと、RealmSwiftのスキームで一度ビルドしました。

Module compiled with swift 3.0.2 cannot be imported in swift 2.x

以前realmのコードをアップデートする前に
レガシーなコードがrealmプロジェクトに使われている旨のエラーが出たため

Use Legacy Swift Language Version

をyesにしていました。今回realmが解決できればswift3対応は済んでいたので、
これをnoに変更して消えました。

以上です。

5
3
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
5
3