不能将领域与对象映射器一起使用 swift 3.0
Can not use realm with object mapper swift 3.0
我将 Realm 与对象映射器一起用于 JSON 解析。当我创建一个同时使用 Object Mapper 和 Realm 的模型 class 时,我得到了编译错误
error:must call a designated initializer of the superclass 'QuestionSet'
import ObjectMapper
import RealmSwift
class QuestionSet: Object, Mappable {
//MARK:- Properties
dynamic var id:Int = 0
dynamic var title:String?
dynamic var shortTitle:String?
dynamic var desc:String?
dynamic var isOriginalExam:Bool = false
dynamic var isMCQ:Bool = false
dynamic var url:String?
//Impl. of Mappable protocol
required convenience init?(map: Map) {
self.init()
}
//mapping the json keys with properties
public func mapping(map: Map) {
id <- map["id"]
title <- map["title"]
shortTitle <- map["short_title"]
desc <- map["description"]
isMCQ <- map["mc"]
url <- map["url"]
isOriginalExam <- map["original_pruefung"]
}
}
如果我在 init 方法中使用 super.init() 会出现编译错误
案例一:
//Impl. of Mappable protocol
required convenience init?(map: Map) {
self.init()
}
error:must call a designated initializer of the superclass 'QuestionSet'
案例二:
//Impl. of Mappable protocol
required convenience init?(map: Map) {
super.init()
}
Convenience initializer for 'QuestionSet' must delegate (with 'self.init') rather than chaining to a superclass initializer (with 'super.init')
案例三:
//Impl. of Mappable protocol
required convenience init?(map: Map) {
super.init()
self.init()
}
error 1: must call a designated initializer of the superclass 'QuestionSet'
Initializer cannot both delegate ('self.init') and chain to a superclass initializer ('super.init')
Convenience initializer for 'QuestionSet' must delegate (with 'self.init') rather than chaining to a superclass initializer (with 'super.init')
我使用这个模式:
我有一个 BaseObject
我所有的 Realm 对象都继承自
open class BaseObject: Object, StaticMappable {
public class func objectForMapping(map: Map) -> BaseMappable? {
return self.init()
}
public func mapping(map: Map) {
//for subclasses
}
}
那么您的 class 将如下所示:
import ObjectMapper
import RealmSwift
class QuestionSet: BaseObject {
//MARK:- Properties
dynamic var id:Int = 0
dynamic var title:String?
dynamic var shortTitle:String?
dynamic var desc:String?
dynamic var isOriginalExam:Bool = false
dynamic var isMCQ:Bool = false
dynamic var url:String?
//mapping the json keys with properties
public func mapping(map: Map) {
id <- map["id"]
title <- map["title"]
shortTitle <- map["short_title"]
desc <- map["description"]
isMCQ <- map["mc"]
url <- map["url"]
isOriginalExam <- map["original_pruefung"]
}
}
我将 Realm 与对象映射器一起用于 JSON 解析。当我创建一个同时使用 Object Mapper 和 Realm 的模型 class 时,我得到了编译错误
error:must call a designated initializer of the superclass 'QuestionSet'
import ObjectMapper
import RealmSwift
class QuestionSet: Object, Mappable {
//MARK:- Properties
dynamic var id:Int = 0
dynamic var title:String?
dynamic var shortTitle:String?
dynamic var desc:String?
dynamic var isOriginalExam:Bool = false
dynamic var isMCQ:Bool = false
dynamic var url:String?
//Impl. of Mappable protocol
required convenience init?(map: Map) {
self.init()
}
//mapping the json keys with properties
public func mapping(map: Map) {
id <- map["id"]
title <- map["title"]
shortTitle <- map["short_title"]
desc <- map["description"]
isMCQ <- map["mc"]
url <- map["url"]
isOriginalExam <- map["original_pruefung"]
}
}
如果我在 init 方法中使用 super.init() 会出现编译错误
案例一:
//Impl. of Mappable protocol
required convenience init?(map: Map) {
self.init()
}
error:must call a designated initializer of the superclass 'QuestionSet'
案例二:
//Impl. of Mappable protocol
required convenience init?(map: Map) {
super.init()
}
Convenience initializer for 'QuestionSet' must delegate (with 'self.init') rather than chaining to a superclass initializer (with 'super.init')
案例三:
//Impl. of Mappable protocol
required convenience init?(map: Map) {
super.init()
self.init()
}
error 1: must call a designated initializer of the superclass 'QuestionSet'
Initializer cannot both delegate ('self.init') and chain to a superclass initializer ('super.init')
Convenience initializer for 'QuestionSet' must delegate (with 'self.init') rather than chaining to a superclass initializer (with 'super.init')
我使用这个模式:
我有一个 BaseObject
我所有的 Realm 对象都继承自
open class BaseObject: Object, StaticMappable {
public class func objectForMapping(map: Map) -> BaseMappable? {
return self.init()
}
public func mapping(map: Map) {
//for subclasses
}
}
那么您的 class 将如下所示:
import ObjectMapper
import RealmSwift
class QuestionSet: BaseObject {
//MARK:- Properties
dynamic var id:Int = 0
dynamic var title:String?
dynamic var shortTitle:String?
dynamic var desc:String?
dynamic var isOriginalExam:Bool = false
dynamic var isMCQ:Bool = false
dynamic var url:String?
//mapping the json keys with properties
public func mapping(map: Map) {
id <- map["id"]
title <- map["title"]
shortTitle <- map["short_title"]
desc <- map["description"]
isMCQ <- map["mc"]
url <- map["url"]
isOriginalExam <- map["original_pruefung"]
}
}