json - 如何使用 Realm Swift 和 ObjectMapper 持久化数组?

标签 json swift realm objectmapper

当我尝试保存来自 JSON 字符串的数组时,出现错误。我尝试使用 RLMArray 但没有成功。

我收到的错误是:

'RLMException', reason: 'Property 'page' is of type 'RLMArray<(null)>' which is not a supported RLMArray object type.

我的模型类:

public class Project: Object, Mappable {

    dynamic var id = 0
    dynamic var user: User!
    dynamic var page: RLMArray!
    dynamic var error_message: String! = ""
    dynamic var status: String! = ""

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

    required convenience public init?(_ map: Map) {
        self.init()
        mapping(map)
    }

    public func mapping(map: Map) {
        user <- map["user"]
        page <- map["page"]
        error_message <- map["error_message"]
        status <- map["status"]

    }
}

JSON 文件:

let parameters = [
                "user": [
                    "username": "Marcus",
                    "password": "123asd"
                ],
                "page": [
                    "home": [
                                [
                                "kind": "navigation",
                                "title": "suite",
                                "image": "ic_suite",
                                "backgroundImage": "ic_background1"
                                ],
                                [
                                    "kind": "navigation",
                                    "title": "jardim",
                                    "image": "ic_jardim",
                                    "backgroundImage": "ic_background2"
                                ]
                            ],
                    "suite": [
                                [
                                "kind": "button",
                                "title": "My Master Suite",
                                "textColor": "0x000000",
                                "textSize": "16"
                                ]
                    ]
                ],
                "status": "success",
                "error_message": ""
        ]
self.project = Mapper<Project>().map(parameters)

最佳答案

你的类继承自 Object ,Realm Swift 的基类,但正在尝试使用 RLMArray ,其接口(interface)中的 Realm Objective-C 类型。您不能以这种方式混合 Realm Swift 和 Realm Objective-C。您应该使用List<T>使用 Realm Swift 时的数组属性。

关于json - 如何使用 Realm Swift 和 ObjectMapper 持久化数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38515354/

相关文章:

android - 如何在 Android 和 ASP.net mvc 之间发送和接收 JSON

json - 使用异步线程时, Realm 数据库大小变为 x100

php - 使用 SendGrid (PHP) 设置抄送参数的多个电子邮件地址

ios - Swift Sprite-Kit - 如何添加通知

仅当从 Xcode 运行时,Swift 包调用/usr/bin/swift 时出现 `Failed to open macho file...Too many levels of symbolic links` 错误

swift - Realm 过滤器并不总能找到数据?

android - Realm 不能迁移

ios - 使用 AFNetworking 2.0 发布时出现错误(Cocoa 错误 3840)(字符 0 周围的值无效)

iOS 应用程序在后台时无法接收位置更新

ios - Realm 不存储 iOS 中的数据