xcode 如何在 Swift 4 中使用 Realm?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44640852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:03:53  来源:igfitidea点击:

How can I use Realm with Swift 4?

swiftxcoderealmxcode9-betaswift4

提问by Lennart P.

I'm trying to run my current project in the new Xcode 9 beta, but when I do so it says Module compiled with Swift 3.1 cannot be imported in Swift 4.0. How can I solve this problem? I'm not using cocoapods.

我正在尝试在新的 Xcode 9 beta 中运行我当前的项目,但是当我这样做时,它说Module compiled with Swift 3.1 cannot be imported in Swift 4.0. 我怎么解决这个问题?我没有使用可可豆。

回答by bdash

Update: As of v2.10.1, released 2017-09-14, Realm's prebuilt binaries include frameworks built with Xcode 9 for Swift 3.2 and 4.0. It's no longer necessary to build them yourself.

更新:截至 2017 年 9 月 14 日发布的 v2.10.1,Realm 的预构建二进制文件包括使用 Xcode 9 为 Swift 3.2 和 4.0 构建的框架。不再需要自己构建它们。

The information below remains relevant to anyone looking to use Realm with prerelease versions of Xcode in the future.

下面的信息仍然适用于任何希望在未来将 Realm 与 Xcode 的预发布版本一起使用的人。



If you're currently integrating Realm's prebuilt binaries, you'll need to switch to building Realm from source in order to support Swift 3.2 and 4.0, as Realm does not publish prebuilt binaries for prerelease versions of Xcode. You can build Realm from source in one of three ways:

如果您当前正在集成 Realm 的预构建二进制文件,则需要切换到从源代码构建 Realm 以支持 Swift 3.2 和 4.0,因为 Realm 不会为 Xcode 的预发布版本发布预构建的二进制文件。您可以通过以下三种方式之一从源代码构建 Realm:

  1. Using CocoaPods.

    CocoaPods always builds dependencies from source.

  2. Using Carthage.

    By default Carthage will attempt to download prebuilt binaries, but will fall back to building from source if the prebuilt binaries are for a different Swift version than the version of Xcode in use.

  3. Build Realm manually from source, and then integrate the built frameworks as you would the prebuilt binaries that Realm provides.

    You can do this by checking out a release tag from Git:

    git clone --recursive https://github.com/realm/realm-cocoa.git
    cd realm-cocoa
    git checkout v2.10.0
    

    Then run whichever of the following commands corresponds to the platform you care about to build the Realm Swift framework for that platform:

    REALM_SWIFT_VERSION=4.0 sh build.sh ios-swift
    REALM_SWIFT_VERSION=4.0 sh build.sh osx-swift
    REALM_SWIFT_VERSION=4.0 sh build.sh watchos-swift
    REALM_SWIFT_VERSION=4.0 sh build.sh tvos-swift
    

    The built frameworks will be placed in the builddirectory within the Realm source, where you can then integrate them as you did the prebuilt binaries that Realm provides.

    These built frameworks should also work with apps using Swift 3.2 due to it using the same compiler as Swift 4.0.

  1. 使用 CocoaPods。

    CocoaPods 总是从源代码构建依赖项。

  2. 使用迦太基。

    默认情况下,Carthage 将尝试下载预构建的二进制文件,但如果预构建的二进制文件用于与正在使用的 Xcode 版本不同的 Swift 版本,则会回退到从源代码构建。

  3. 从源代码手动构建 Realm,然后像集成 Realm 提供的预构建二进制文件一样集成构建的框架。

    你可以通过从 Git 检出一个发布标签来做到这一点:

    git clone --recursive https://github.com/realm/realm-cocoa.git
    cd realm-cocoa
    git checkout v2.10.0
    

    然后运行以下与您关心的平台相对应的命令,为该平台构建 Realm Swift 框架:

    REALM_SWIFT_VERSION=4.0 sh build.sh ios-swift
    REALM_SWIFT_VERSION=4.0 sh build.sh osx-swift
    REALM_SWIFT_VERSION=4.0 sh build.sh watchos-swift
    REALM_SWIFT_VERSION=4.0 sh build.sh tvos-swift
    

    构建的框架将放置在buildRealm 源代码中的目录中,然后您可以像集成 Realm 提供的预构建二进制文件一样在其中集成它们。

    这些构建的框架也应该适用于使用 Swift 3.2 的应用程序,因为它使用与 Swift 4.0 相同的编译器。

回答by Smartcat

As a followup to bdash's item 3 about how to build Realm manually from source, and to answer addzo's question about the xcodebuild error (that I ran into as well): Be sure that the iPhone 6 simulator is set up for your Xcode 9 to avoid that error. I suppose Realm's build scripts must target it. This solved it for me, anyhow.

作为 bdash 关于如何从源代码手动构建 Realm 的第 3 项的后续内容,并回答 addzo 关于 xcodebuild 错误的问题(我也遇到过):请确保为您的 Xcode 9 设置了 iPhone 6 模拟器以避免那个错误。我想 Realm 的构建脚本必须以它为目标。无论如何,这为我解决了它。