如何在Swift 4中使用Realm? [英] How can I use Realm with Swift 4?

查看:177
本文介绍了如何在Swift 4中使用Realm?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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.

以下信息仍然与希望将来在Xcode的预发行版中使用Realm的任何人有关.

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

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

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:

  1. 使用CocoaPods.

  1. Using CocoaPods.

CocoaPods始终从源构建依赖关系.

CocoaPods always builds dependencies from source.

使用迦太基.

默认情况下,迦太基将尝试下载预构建的二进制文件,但是如果预构建的二进制文件所使用的Swift版本与所使用的Xcode版本不同,则会从源代码回落到构建版本.

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.

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

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

您可以通过从Git中签出发行标签来完成此操作:

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

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

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

构建的框架将放置在Realm源文件的build目录中,然后您可以像处理Realm提供的预构建二进制文件一样将它们集成.

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

这些构建的框架也应与使用Swift 3.2的应用程序一起使用,因为它使用的是与Swift 4.0相同的编译器.

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

这篇关于如何在Swift 4中使用Realm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆