跳转至

Rx-mapArray

/// - Parameters:
///   - type: 模型类型
///   - modelKey: 模型路径
public func mapArray<T: Modelable>(_ type: T.Type, modelKey: String? = nil) -> Observable<[T]>

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

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


Example

let rxRequest: Single<Response>

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

转模型数组

rxRequest.mapArray(MMModel.self, modelKey: "retBody")
    .subscribe(onSuccess: { models in
        let name = models[0].name
        print("count -- \(models.count)")
        print("name -- \(name)")
    }).disposed(by: disposeBag)

打印结果

count -- 4
name -- MoyaMapper