如何保存一个数组到领域对象 [英] How to save an array to a Realm Object

查看:150
本文介绍了如何保存一个数组到领域对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的使用领域。有没有一种简单的方法来保存数组的领域对象?我从一个JSON REST调用接收我的数据:

I am new to using Realm. Is there an easy way to save an array to a realm object? I am receiving my data from a JSON REST call:

class SomeClass: RLMObject {

    dynamic var id = 0
    dynamic var name = ""
    dynamic var array: NSArray


    func checkForUpdates() {
        // Download JSON data here... The results have an array inside of them.

        SomeClass.createOrUpdateInDefaultRealmWithObject(SomeNSDictionary)         


    }

    override class func primaryKey() -> String! {
        return "id"
    }
}

是否有可能到数组保存在域的JSON结果?

Is it possible to save the array in the JSON results in Realm?

感谢。

推荐答案

领域具有特殊的 RLMArray 类型,它允许存储集合 RLMObject 的绑在父 RLMObject 。例如,假设你有以下JSON:

Realm has a special RLMArray type, which allows storing a collection of RLMObject's tied to a parent RLMObject. For example, say you had the following JSON:

{
  "name": "John Doe",
  "aliases": [
    {"alias": "John"},
    {"alias": "JD"}
  ]
}

您可以与以下类建模的:

You could model this with the following classes:

class Alias: RLMObject {
  dynamic var alias = ""
}

class Person: RLMObject {
  dynamic var name = ""
  dynamic var aliases = RLMArray(objectClassName: "Alias")
}

所以,你可以简单地创建一个对象具有以下API调用:

So you could simply create a Person object with the following API call:

Person.createInRealm(realm, withObject: jsonObject)

您可以了解更多有关如何 RLMArray 期从境界的参考文档工作:<一href=\"http://realm.io/docs/cocoa/0.80.0/api/Classes/RLMArray.html\">http://realm.io/docs/cocoa/0.80.0/api/Classes/RLMArray.html

You can learn more about how RLMArrays work from Realm's reference documentation: http://realm.io/docs/cocoa/0.80.0/api/Classes/RLMArray.html

这篇关于如何保存一个数组到领域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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