swift 2.1 : Unable to map JSON object to Realm object using ObjectMapper

标签 swift swift2 realm swifty-json objectmapper

我正在尝试使用 ObjectMapper 将我的 JSON 对象映射到 Realm 对象,但我总是得到 nil。

import RealmSwift
import ObjectMapper

class Notification: Object, Mappable {
dynamic var id = 0
dynamic var isProcessed = false
dynamic var type = ""
var supersedes : Int?

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

// Mappable
func mapping(map: Map) {
    id <- map["id"]
    isProcessed <- map["isProcessed"]
    type <- map["type"]
    supersedes <- map["supersedes"]
}
}

我正在使用以下代码行将传入的 JSON 映射到上面的 Realm 对象。

let notif = Mapper<Notification>().map(notification) // notif here is nil

通知是一个JSON对象(使用了SwiftyJSON库)

示例通知 JSON:

  {
        data =             {
            buyerInfo =                 {
                image = "";
                name = "";
                userId = UID2268351488;
            };
            sellerSKUs =                 (
                                    {
                    id = SSK236123228808967424;
                    price =                         {
                        amount = 888;
                        currency = THB;
                    };
                    quantity = 2;
                },
                                    {
                    id = SSK563365068895040768;
                    price =                         {
                        amount = 6865;
                        currency = THB;
                    };
                    quantity = 1;
                }
            );
            subOrderId = SOD751798094080240;
        };
        id = 39038;
        isProcessed = 0;
        supersedes = "<null>";
        type = PendingSubOrderConfirmationNotification;
    },

请帮忙!

最佳答案

您不能以这种方式组合 ObjectMapper 和 SwiftyJSON。 您可以提供来自 SwityJSON JSON 结构的 rawValue 作为 ObjectMapper 的 Mapper.map 的参数,或者您只使用 NSJSONSerialization .

关于 swift 2.1 : Unable to map JSON object to Realm object using ObjectMapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34328342/

相关文章:

ios - swift 4 : Unrecognized selector sent to instance when calling a function inside another class

ios - Sprite 节点随机渲染......需要帮助

ios - 如何在 Swift 2 中使用可选绑定(bind)

ios - Realm Swift - 对象中可变数量的值

ios - 是否可以在两个平台之间共享相同的 Realm 数据库文件?

ios - 识别长按 UITableViewCell 的真实索引路径

ios - 当键盘出现时滑动桌面 View

unit-testing - 如何在 Swift 中设置单元测试?

variables - 如何在 WatchOS 2 和 Swift 中将变量从一个 View Controller 传递到另一个 View Controller

ios - 将 NSArray 传递到 Realm 查询 objectsWhere