嵌套数组在Swift的realm.create(value:JSON)中引发错误 [英] Nested Arrays throwing error in realm.create(value: JSON) for Swift

查看:69
本文介绍了嵌套数组在Swift的realm.create(value:JSON)中引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Swift项目中使用Realm,并且有一个带有两个嵌套属性的相当长的JSON文件.我知道,为了让Realm直接使用此序列化的JSON数据,属性需要完全匹配(

I'm using Realm in my Swift project and have a rather long JSON file with a couple of nested properties. I'm aware that in order for Realm to use this serialized JSON data directly, the properties need to match exactly (https://realm.io/docs/swift/latest/#json).

但是,因为领域列表需要有一个对象而不是字符串,所以我必须使用诸如List之类的东西,其中Requirement是一个拥有单个字符串(称为值")的领域对象.

But because Realm Lists need to have an Object instead of a String, I have to use something like List with Requirement being a Realm Object that holds a single String called 'value'.

当我运行此代码时:

  try! realm.write {
    let json = try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
    let exhibit = Exhibit(value: json)
    exhibit.id = "1"
    realm.add(exhibit, update: true)
  }

我收到此错误消息: ***由于未捕获的异常'RLMException'而终止应用程序,原因:'req1'初始化类型为'Requirements'的对象:缺少键'value'

I get this error message: *** Terminating app due to uncaught exception 'RLMException', reason: 'req1' to initialize object of type 'Requirements': missing key 'value''

这是我在其中输入的JSON的简化版本:

Here's a shortened version of the JSON I'm feeding in there:

{
    "exhibit_name": "test1",
    "requirements": [
        "req1",
        "req2"
    ],
    "geofence": {
        "latitude": 36.40599779999999,
        "longitude": -105.57696279999999,
        "radius": 500
    }
}

我的Realm模型类是这样的:

And my Realm model classes are this:

class Exhibit: Object {

  override static func primaryKey() -> String? {
    return "id"
  }

  dynamic var id = "0" //primary key
  dynamic var exhibit_name: String = ""
  let requirements = List<Requirements>()
  dynamic var geofence: Geofence?

}

class Geofence: Object {
  dynamic var latitude: Float = 0.0
  dynamic var longitude: Float = 0.0
  dynamic var radius: Float = 0.0
}

class Requirements: Object {
  dynamic var value = ""
}

我发现有趣的是,因为这是一本字典,所以我对Geofence属性没有任何错误.

I find it interesting that I'm not getting any errors for the Geofence property, since that's a dictionary.

如何设置需求模型以使其正常工作?

How do I set up the Requirements model to make this work properly?

推荐答案

不幸的是,您不能仅以不同的方式设置Requirements模型,这将使​​您可以将JSON直接映射到Realm对象.

Unfortunately you can't just setup your Requirements model in a different way, which would allow you to directly map your JSON to Realm objects.

init(value: AnyObject)初始化程序需要一个字典(其中键是对象属性的名称)或一个数组(其中属性值的排列顺序与对象模型中定义的顺序相同).会为相关对象递归调用此初始值设定项.

The init(value: AnyObject) initializer expects either a dictionary, where the keys are the names of your object properties, or an array, where the property values are ordered in the same like they are defined in your object model. This initializer is recursively called for related objects.

因此,要使其正常工作,您将需要转换JSON,以便将字符串值嵌套到字典或数组中.在您的特定情况下,您可以实现如下所示:

So to make that work, you will need to transform your JSON, so that you nest the string values into either dictionaries or arrays. In your specific case you could achieve that like seen below:

…
var jsonDict = json as! [String : AnyObject]
jsonDict["requirements"] = jsonDict["requirements"].map { ["value": $0] }
let exhibit = Exhibit(value: jsonDict)
…

旁注

我建议您为Realm模型对象类使用单数名称(此处为Requirement代替Requirements),因为每个对象仅代表一个实体,即使您仅在一对多关系中使用它们.

Side Note

I'd recommend using singular names for your Realm model object classes (here Requirement instead Requirements) as each object just represent a single entity, even if you use them only in to-many relationships.

这篇关于嵌套数组在Swift的realm.create(value:JSON)中引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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