我正在尝试使用 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/