Search code examples
iosswiftxcoderealmcocoapods

Move from manual install to use Realm through Cocoapods in Xcode


I’m currently using Realm in one of my projects, where I manually installed it. Everything was working fine until I updated Xcode from 10.1 to 10.2. Now I keep getting the following error.

Module compiled with Swift 4.2 cannot be imported by the Swift 5.0 compiler: /Users/userName/MyApp/RealmSwift.framework/Modules/RealmSwift.swiftmodule/i386.swiftmodule

I believe this has to do with the fact that when I try to replace the Realm frameworks in my project with the newest Realm compiled for Xcode 10.2, Xcode doesn’t copy the files, it just references them even when having the Copy Files If Needed option checked.

Anyways, I now would like to try using Realm through Cocoapods to eliminate this kind of issues every time I update Xcode, so my questions are…

  1. What do I need to change in Xcode in order for me to move from manual install to use Realm through Cocoapods?

  2. What do I need in Framework Search Paths?

  3. Do I need to remove the Run Script Phase bash "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}/Realm.framework/strip-frameworks.sh"?

  4. What do I need in my Pod file? I’m more interested to see how I would incorporate Realm for my WatchApp Extension since I couldn’t find any info in the documentation.

Here is how my Pod file looks like right now.

Pod File

#platform :ios, '9.3'

target 'MyApp' do
  use_frameworks!

  # Pods for MyApp
  pod 'Firebase/Core'
  pod 'NVActivityIndicatorView'

  target 'MyAppTests' do
    inherit! :search_paths
  end

  target 'MyAppUITests' do
    inherit! :search_paths
  end
end

target 'MyApp Watch App' do
  use_frameworks!

end

target 'MyApp Watch App Extension' do
  use_frameworks!

end

FYI - I’m already using Cocoapods in my project but not for Realm yet. Also, I’m using Realm for iOS and WatchApp Extension in this project.


Solution

  • If you already have the workspace created by cocoapods, you don't need to modify your project settings, when you add a new pod to your podfile, cocoapods will automatically handle linking that to your project correctly.

    Simply remove the manually added Realm framework files, remove the custom Run Script build phase containing "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}/Realm.framework/strip-frameworks.sh", then run pod update and you should be good to go.

    As for the original issue of

    Module compiled with Swift 4.2 cannot be imported by the Swift 5.0 compiler: /Users/userName/MyApp/RealmSwift.framework/Modules/RealmSwift.swiftmodule/i386.swiftmodule

    , when using precompiled frameworks, you need to wait until the developer of the framework updates the precompiled versions for Swift 5 (or the newest version you want to switch your project to) before you can use them.

    Since cocoapods compiles all dependencies from source on your own machine, this issue of incompatible Swift compiler versions cannot arise and you can even control which Swift version to use for each project specifically from your podfile.