2017-06-06 3 views
1

J'utilise Realm avec Object Mapper pour l'analyse JSON. Lorsque je crée une classe de modèle qui utilise à la fois l'objet Mapper et Realm que je reçois erreur de compilationImpossible d'utiliser le domaine avec le mappeur d'objets swift 3.0

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"] 
    } 
} 

si j'utilise super.init() dans la méthode init que je reçois l'erreur de compilation

cas 1:

//Impl. of Mappable protocol 
required convenience init?(map: Map) { 
    self.init() 
} 

error:must call a designated initializer of the superclass 'QuestionSet'

Cas n ° 2:

//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')

Cas n ° 3:

//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')

+0

Vous avez besoin d'un constructeur par défaut ou quelle que soit votre langue (Swift) l'appelle. – EpicPandaForce

+0

@EpicPandaForce Si j'utilise la méthode init() par défaut que je dois implémenter les méthodes init restantes de Object. Que je ne veux pas mettre en œuvre. Donc, ici, la commodité init() est utilisée –

+0

Hmm ... alors je ne suis pas sûr. Ignorez-moi, Swift n'est pas mon fort: D – EpicPandaForce

Répondre

1

-je utiliser ce modèle:

J'ai un BaseObject que tous mes objets de royaume héritent de

open class BaseObject: Object, StaticMappable { 

    public class func objectForMapping(map: Map) -> BaseMappable? { 
     return self.init() 
    } 

    public func mapping(map: Map) { 
     //for subclasses 
    } 
} 

Ensuite, votre classe ressemblerait à ceci:

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"] 
    } 
}