LoginSignup
2
2

More than 5 years have passed since last update.

HimotokiとObjectMapperをAPIKitで使おうと思っている話

Posted at

最初からHimotokiの想定でAPIKitをベースにクライアントを作ったけど、結局Realmを使いたくてObjectMapperにしておけばよかった!となった私です。

結局共存の必要はなく。Himotokiだけでなんとかした。

最初はRequestTypeで

extension RJAuthRequestType where Response: Decodable  {
    func responseFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) throws -> Response {
        return try decodeValue(object)
    }
}

extension RJAuthRequestType where Response: Mappable{

    func responseFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) -> Self.Response? {
        guard let dictionary = object as? [String: AnyObject] else {
            return nil
        }

        let mapper = Mapper<Response>()
        guard let object = mapper.map(dictionary) else {
            return nil
        }

        return object
    }
}

こんな感じで書いておけばResponseがMappableだろうとDecodableだろうといけると思っていたけどずっと does not conform to protocol って怒られていて、結局himotoki一本にした。

ただ保存するModelの形式をstructのままではRMLObjectは使えないので、

final class TeacherModel:RLMObject{
...
}

extension TeacherModel:Decodable{
    static func decode(e: Extractor) throws -> TeacherModel {
        var teacher = TeacherModel()
            teacher.tutorId =       try   e <|  "tutor_id"
        return teacher
    }
}

こんな感じにしてみた。これでビルドもパースも無事できた、でもちょっとHimotokiの恩恵が・・・

2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2