跳转至

Rx-mapObject

/// - Parameters:
///   - type: 模型类型
///   - modelKey: 模型数据路径
func mapObject<T: Modelable>(_ type: T.Type, modelKey: String? = nil) ->  Observable<T>

func mapObject<T: Modelable>(_ type: T.Type, modelKey: String? = nil) ->  Single<T>

详细说明请参考 Core 目录下的 mapObject ,这里就不再赘述


Example

let rxRequest: Single<Response>

success-obj

struct MMModel: Modelable {
    var name : String
    var organization : String
    var logoUrl : String
    var github : String
    var avatar : String
    var author : String
    var age : Int

    init(_ json: JSON) {
        name = json["name"].stringValue
        organization = json["organization"].stringValue
        logoUrl = json["logoUrl"].stringValue
        github = json["github"].stringValue
        avatar = json["avatar"].stringValue
        author = json["author"].stringValue
        age = json["age"].intValue
    }
}

转模型

rxRequest.mapObject(MMModel.self, modelKey: "retBody")
    .subscribe(onSuccess: { (model) in
        print("name -- \(model.name)")
        print("github -- \(model.github)")
        print("author -- \(model.author)")
        print("age -- \(model.age)")
    }).disposed(by: disposeBag)

打印结果

name -- MoyaMapper
github -- https://github.com/LinXunFeng
author -- LinXunFeng
age -- 23