使用Realm的RLMArray存储字符串数组 [英] Storing an array of strings using Realm's RLMArray

查看:3185
本文介绍了使用Realm的RLMArray存储字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何使用Realm存储字符串数组?我正在尝试将以下响应正确映射到Realm中:

Does anyone know how you can use Realm to store an array of strings? I'm trying to map the following response into Realm correctly:

"zoneInfo": {
    "tariffInfoLines": [
        "In this city you pay per minute."
    ]
}

我们有一个包含tariffInfoLines数组的zoneInfo对象。此tariffInfoLines数组包含字符串。在Realm中,有两种不同的变量类型用于存储数据。第一个是 RLMObject ,它允许你的标准NSString,int,long等等。

We have a zoneInfo object that contains a tariffInfoLines array. This tariffInfoLines array contains strings. In Realm there are two different variable types for storing data. The first is RLMObject which allows your standard NSString, int, long etc.

第二种类型是 RLMArray ,用于数组(不支持NSArray)。您必须为数组提供一个类型,该类型必须是RLMObject的子类。到目前为止,我们通过使用ABCRealmString对象解决了这个问题,如下所示:

The second type is RLMArray, which is used for arrays (as NSArray is not supported). You have to give the array a type, which must be a class that subclasses RLMObject. We have so far got around this by using a ABCRealmString object, as shown below:

@property RLMArray<ABCRealmString> *tariffInfoLines;

ABCRealmString包含一个NSString属性(它基本上是一个包装器):

ABCRealmString contains an NSString property (it is basically a wrapper):

@property NSString *value;

然而,这意味着当Realm尝试映射响应以保留数据时,它正在寻找对于键值(属性的名称)的值。它似乎预计会有类似以下的回复:

However what this means is that when Realm tries to map the response to persist the data, it is looking for a value for the key "value" (the name of the property). It appears that it expects a response similar to the following:

"zoneInfo": {
    "tariffInfoLines": [
        {
            "value": "In this city you pay per minute."
        },
    ]
}

在项目中,我们让它适用于以下结构:

In the project, we have it working for the following structure:

"userOptions": [
    {
        "wantsEmailNotifications": true,
        "wantsPushNotifications": false
    },
]

它有一个数组,其中的对象具有Realm可以映射到的清晰键值对。 zoneInfo结构似乎是唯一一个我们有一组数组的地方,里面没有它们在一个对象内或有任何键。

This has an array, with objects inside that have clear key value pairs that Realm can map to. The zoneInfo structure appears to be the only place that we have an array with sets of values inside without them being inside an object or having any keys.

如果有人可以摆脱一些关于这是否可以使用Realm,或者是否需要API更改以匹配Realm可以映射的结构。

If anyone could shed some light on this, regarding if this is possible using Realm, or whether an API change is required to match a structure that Realm can map.

推荐答案

来自 github问题回复的交叉发布:尽管如此示例演示如何在Realm模型上存储平面字符串数组,您可以扩展此模式以存储从整数数组到本机Swift枚举的任何内容。基本上你可以映射到Realm中可表示类型的任何东西。

Cross posting from the github issue response: Although this example demonstrates how to store flat arrays of strings on a Realm model, you can extend this pattern to store anything from arrays of integers to native Swift enum's. Basically anything that you can map to a representable type in Realm.

class RealmString: Object {
    dynamic var stringValue = ""
}

class Person: Object {
    var nicknames: [String] {
        get {
            return _backingNickNames.map { $0.stringValue }
        }
        set {
            _backingNickNames.removeAll()
            _backingNickNames.appendContentsOf(newValue.map({ RealmString(value: [$0]) }))
        }
    }
    let _backingNickNames = List<RealmString>()

    override static func ignoredProperties() -> [String] {
        return ["nicknames"]
    }
}

// Usage...

let realm = try! Realm()
try! realm.write {
    let person = Person()
    person.nicknames = ["John", "Johnny"]
    realm.add(person)
}

for person in realm.objects(Person) {
    print("Person's nicknames: \(person.nicknames)")
}

// Prints:
// Person's nicknames: ["John", "Johnny"]

这篇关于使用Realm的RLMArray存储字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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