跳转至

Core-mapObjResult

/// Response -> (MoyaMapperResult, Model)
///
/// - Parameters:
///   - type: 模型类型
///   - params: 自定义解析的设置回调
/// - Returns: (MoyaMapperResult, Model)
func mapObjResult<T: Modelable>(_ type: T.Type, params: ModelableParamsBlock? = nil) -> (MoyaMapperResult, T)

Response 转换为 元祖 ((Bool, String), Model)

  • type 遵守了 Modelable 协议的模型类型
  • params 用于指定特殊取值路径,如果为 nil ,则取当前 ResponseModelableParameterType 为取值依据

ps: 当前ResponseModelableParameterType 可以通过 MoyaMapperPlugin 进行统一设置


Example

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

// MARK:- 自定义网络结果参数
struct CustomNetParameter: ModelableParameterType {
    var successValue = "000"
    var statusCodeKey = "retStatus"
    var tipStrKey = "retMsg"
    var modelKey = "retBody"
}

转模型

let (result, model) = response.mapObjResult(MMModel.self) { CustomNetParameter() }
print("result -- \(result)")
print("name -- \(model.name)")

打印结果

result -- (true, "获取数据成功")
name -- MoyaMapper