领域+对象映射器+ SwityJSON [英] Realm + Object Mapper + SwityJSON

查看:101
本文介绍了领域+对象映射器+ SwityJSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在映射对象方面需要帮助

领域模型: https://gist.github.com/n1tesh/7d6c6e155285dd6b39c8edba76f6eba5

这就是我的工作方式

    // write request result to realm database
    let entries = json["data"]
    realm.beginWrite()
        let entry: ChatGroups = Mapper<ChatGroups>().map(JSONObject: entries)!
        realm.add(entry, update: true)

    do {
        try realm.commitWrite()
    } catch {

    }

JSON响应: https://gist.github.com/n1tesh/bf84cbd930f8c76b340f21723a217ebe

但是我遇到了错误 fatal error: unexpectedly found nil while unwrapping an Optional value

请帮我解决我做错的事情.

解决方案

创建一个类以将Array转换为List,因为Realm不接受数组.

import ObjectMapper
import RealmSwift

public class ListTransform<T:RealmSwift.Object> : TransformType where T:Mappable {
    public typealias Object = List<T>
    public typealias JSON = [AnyObject]

    let mapper = Mapper<T>()

    public init(){}

    public func transformFromJSON(_ value: Any?) -> Object? {
        let results = List<T>()
        if let value = value as? [AnyObject] {
            for json in value {
                if let obj = mapper.map(JSONObject: json) {
                    results.append(obj)
                }
            }
        }
        return results
    }

    public func transformToJSON(_ value: Object?) -> JSON? {
        var results = [AnyObject]()
        if let value = value {
            for obj in value {
                let json = mapper.toJSON(obj)
                results.append(json as AnyObject)
            }
        }
        return results
    }
}

然后在ChatGroups类中,您必须调用Transform函数进行转换,进行以下更改:

updated_by              <- map["updated_by"]
members                 <- map["member"]

对此:

updated_by              <- (map["updated_by"], ListTransform<QuorgUser>())
members                 <- (map["member"], ListTransform<GroupMember>())

I need help in mapping my object

Realm Model: https://gist.github.com/n1tesh/7d6c6e155285dd6b39c8edba76f6eba5

This is how I'm doing

    // write request result to realm database
    let entries = json["data"]
    realm.beginWrite()
        let entry: ChatGroups = Mapper<ChatGroups>().map(JSONObject: entries)!
        realm.add(entry, update: true)

    do {
        try realm.commitWrite()
    } catch {

    }

JSON Response: https://gist.github.com/n1tesh/bf84cbd930f8c76b340f21723a217ebe

But i'm getting error fatal error: unexpectedly found nil while unwrapping an Optional value

Please help me out with what I'm doing wrong.

解决方案

Create a class to transform Array to List, because the Realm doesn't accept arrays.

import ObjectMapper
import RealmSwift

public class ListTransform<T:RealmSwift.Object> : TransformType where T:Mappable {
    public typealias Object = List<T>
    public typealias JSON = [AnyObject]

    let mapper = Mapper<T>()

    public init(){}

    public func transformFromJSON(_ value: Any?) -> Object? {
        let results = List<T>()
        if let value = value as? [AnyObject] {
            for json in value {
                if let obj = mapper.map(JSONObject: json) {
                    results.append(obj)
                }
            }
        }
        return results
    }

    public func transformToJSON(_ value: Object?) -> JSON? {
        var results = [AnyObject]()
        if let value = value {
            for obj in value {
                let json = mapper.toJSON(obj)
                results.append(json as AnyObject)
            }
        }
        return results
    }
}

Then in your ChatGroups class you have to call the Transform function to make the transformation, make this change:

updated_by              <- map["updated_by"]
members                 <- map["member"]

to this:

updated_by              <- (map["updated_by"], ListTransform<QuorgUser>())
members                 <- (map["member"], ListTransform<GroupMember>())

这篇关于领域+对象映射器+ SwityJSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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