简体   繁体   English

在Swift应用程序中安装Realm

[英]Install Realm in a Swift App

I am trying to add Realm to my app written in swift. 我正在尝试将Realm添加到我用swift编写的应用程序中。 I have followed the tutorial and I can't seem to get it to work. 我按照教程,似乎无法让它工作。 The biggest problem is that when I try to import Realm I get No such module 'Realm' I don't know what else to try. 最大的问题是,当我尝试导入Realm时,我得到No such module 'Realm'我不知道还有什么可以尝试。 You can see my efforts below. 你可以在下面看到我的努力。

You can see the instructions here: http://realm.io/docs/cocoa/0.85.0/#swft 您可以在此处查看说明: http//realm.io/docs/cocoa/0.85.0/#swft

I have also copied the instructions below: 我还复制了以下说明:

Due to the current lack of proper infrastructure for Swift dependency management, using Realm in your project requires the following steps: 由于当前缺乏适当的Swift依赖管理基础结构,因此在项目中使用Realm需要执行以下步骤:

  1. Add Realm as a submodule by opening the Terminal, cd-ing into your top-level project directory, and entering the command git submodule add git@github.com:realm/realm-cocoa.git 通过打开终端,进入顶层项目目录,然后输入命令git submodule add git@github.com:realm / realm-cocoa.git,将Realm添加为子模块
  2. Open the realm-cocoa folder, and drag Realm.xcodeproj into the file navigator of your Xcode project. 打开realm-cocoa文件夹,将Realm.xcodeproj拖到Xcode项目的文件导航器中。
  3. In Xcode, navigate to the target configuration window by clicking on the blue project icon, and selecting the application target under the “Targets” section in the sidebar. 在Xcode中,通过单击蓝色项目图标导航到目标配置窗口,然后在侧栏的“目标”部分下选择应用程序目标。
  4. In the tab bar at the top of that window, open the “Build Phases” panel. 在该窗口顶部的选项卡栏中,打开“构建阶段”面板。
  5. Expand the “Target Dependencies” gorup, and add Realm's iOS framework. 展开“Target Dependencies”gorup,并添加Realm的iOS框架。
  6. Expand the “Link Binary with Libraries” group, and add Realm's iOS framework as well as libc++.dylib. 展开“Link Binary with Libraries”组,添加Realm的iOS框架以及libc ++。dylib。
  7. Click on the + button at the top left of the panel and select “New Copy Files Phase”. 单击面板左上角的+按钮,然后选择“新建复制文件阶段”。 Rename this new phase to “Copy Frameworks”, set the “Destination” to “Frameworks”, and add Realm.framework. 将此新阶段重命名为“Copy Frameworks”,将“Destination”设置为“Frameworks”,然后添加Realm.framework。
  8. Drag the file at realm-cocoa/Realm/Swift/RLMSupport.swift into the file navigator of your Xcode project, unchecking the “Copy items if needed” checkbox. 将realm-cocoa / Realm / Swift / RLMSupport.swift中的文件拖到Xcode项目的文件导航器中,取消选中“如果需要,复制项目”复选框。

Below is what it looks like in my project: 以下是我的项目中的样子:

在此输入图像描述在此输入图像描述在此输入图像描述在此输入图像描述在此输入图像描述

I am not sure exactly why this isn't working, but here is a workaround: 我不确定为什么这不起作用,但这是一个解决方法:

  1. Follow the latest instructions. 按照最新说明操作。

  2. Create a bridging header, for example by 创建一个桥接头,例如

    • Add a new Objective-C class to your xcode project. 在xcode项目中添加一个新的Objective-C类。
    • Agree to have a bridging header created 同意创建一个桥接头
    • Delete the Objective-C class 删除Objective-C类

  3. Add this in the bridging header: 在桥接标题中添加:

    #import "Realm/Realm.h"

  4. Remove any Import Realm statements from your code, including from RLMSupport.swift 从代码中删除任何Import Realm语句,包括RLMSupport.swift

  5. Now it should work. 现在它应该工作。 For example, I test with putting this in my ViewController.swift 例如,我将它放在我的ViewController.swift中进行测试

     import UIKit class Person: RLMObject { dynamic var name = "" dynamic var birthdate = NSDate(timeIntervalSince1970: 1) } class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let author = Person() author.name = "David Foster Wallace" // Get the default Realm let realm = RLMRealm.defaultRealm() // Add to the Realm inside a transaction realm.beginWriteTransaction() realm.addObject(author) realm.commitWriteTransaction() // Print all Persons println(Person.allObjects()) } } 

Which prints: 哪个印刷品:

RLMArray <0x7a243760> (
    [0] Person {
        name = David Foster Wallace;
        birthdate = 1970-01-01 00:00:01 +0000;
    }
)

I have been talking with the guys at Realm, and it turns out that the latest instructions don't work with Realm <= 0.85 They changed they way the build the framework and it won't work anymore. 我一直在与Realm的人交谈,事实证明最新的指令不适用于Realm <= 0.85他们改变了构建框架的方式,它将不再起作用。 They said they will release 0.86 later today that should fix the problems anyone is having with Swift. 他们说他们将在今天晚些时候发布0.86,这将解决任何人对Swift的问题。 In the meantime I have a test project that anyone can take the latest framework from. 与此同时,我有一个测试项目,任何人都可以从中获取最新的框架。 https://github.com/smitt04/testRealm https://github.com/smitt0​​4/testRealm

Version 0.86 is now out and this is no longer an issue. 版本0.86现已淘汰,这不再是一个问题。

The Swift installation instructions were long and convoluted, so I'm not surprised you and several other users ran into issues. Swift的安装说明冗长且令人费解,所以我并不感到惊讶,其他几个用户也遇到了问题。

Please follow the latest installation instructions here . 请按照此处的最新安装说明操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM