跳转至

Core-mapArrayResult

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

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

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

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


Example

success-array

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

MoyaMapperResult + 模型数组

let (result, models) response.mapArrayResult(MMModel.self) { CustomNetParameter() }
let name = models[0].name
print("result -- \(result)")
print("count -- \(models.count)")
print("name -- \(name)")

打印结果

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