我正在尝试使用 ObjectMapper 将我的 JSON 对象映射到 Realm 对象,但我一直为零。
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;
},
请帮忙!