【问题标题】:Realm: Bundling a Realm with an App领域:将领域与应用程序捆绑在一起
【发布时间】:2016-09-22 08:56:58
【问题描述】:

我想用多个数据库发布我的应用程序。

1) 我有一个随机生成的“.csv”文件,使用 Realm Browser 转换为“default.realm”。

2) 我把它放在 /project_name/project_name/Resources 中,然后放到 Xcode 中的项目文件中。

3) 我勾选了“复制捆绑资源”

4)创建

import RealmSwift

class CarItem: Object {
    dynamic var id = String()
    dynamic var first_name = String()
}

在 ViewDidLoad 中想要从文件中返回结果

override func viewDidLoad() {
super.viewDidLoad()

   let conf = Realm.Configuration(
        fileURL: NSBundle.mainBundle().URLForResource("default", withExtension: "realm"),
        readOnly: true)

   let realm = try! Realm(configuration: conf)
   let results = realm.objects(Item.self)
   print(results)
}

5)但是结果是空的(数据库有 1000 行都充满了数据)....我错了什么?

【问题讨论】:

    标签: swift swift2 realm


    【解决方案1】:

    CarItem.self 替换 Item.self

    【讨论】:

    • 为什么在应用程序委托中?是否应该将我的方法从 ViewDidLoad 移动到应用委托?
    • btw...错误是错误的Class.Object名称,将其更改为“MOCK_DATA”..与从互联网生成的数据相同。
    猜你喜欢
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    相关资源
    最近更新 更多