[英]How to save an array to a Realm Object
I am new to using Realm. 我是使用Realm的新手。 Is there an easy way to save an array to a realm object?
有没有一种简单的方法将数组保存到领域对象? I am receiving my data from a JSON REST call:
我从JSON REST调用接收数据:
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"
}
}
Is it possible to save the array in the JSON results in Realm? 是否可以将数组保存在Realm中的JSON结果中?
Thanks. 谢谢。
Realm has a special RLMArray
type, which allows storing a collection of RLMObject
's tied to a parent RLMObject
. 领域具有特殊
RLMArray
类型,它允许存储集合RLMObject
绑在父母的RLMObject
。 For example, say you had the following JSON: 例如,假设您有以下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")
}
So you could simply create a Person
object with the following API call: 所以你可以使用以下API调用创建一个
Person
对象:
Person.createInRealm(realm, withObject: jsonObject)
You can learn more about how RLMArray
s work from Realm's reference documentation: http://realm.io/docs/cocoa/0.80.0/api/Classes/RLMArray.html 您可以从Realm的参考文档中了解有关
RLMArray
如何工作的更多信息: http : RLMArray
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.