MoyaMapper 基于 Moya 和 SwiftyJSON 的快速解析模型工具 项目简介
MoyaMapper是基于 Moya 和 SwiftyJSON 封装的工具,以 Moya 的 plugin 的方式来实现间接解析,支持RxSwift 。Usage一、注入定义一个继承于ModelableParameterType的类final class NetParameter : ModelableParameterType {
var successValue: String {
return "false"
}
var statusCodeKey: String {
return "error"
}
var tipStrKey: String {
return ""
}
var modelKey: String {
return "results"
}
}以plugin的方式传递给MoyaProviderlet lxfNetTool = MoyaProvider<LXFNetworkTool>(plugins: [MoyaMapperPlugin(NetParameter())])二、定义模型创建一个继承于Modelable的结构体struct MyModel: Modelable {
var _id = ""
......
init?(_ json: JSON) {
}
mutating func mapping(_ json: JSON) {
self._id = json["_id"].stringValue
......
}
}三、解析这里只贴出主要代码NormallxfNetTool.request(.data(type: .all, size: 10, index: 1)) { result in
guard let response = result.value else { return }
// Models
guard let models = try? response.mapArray(MyModel.self) else {return}
for model in models {
print("id -- \(model._id)")
}
// 使用自定义模型参数类
/*
guard let models = try? response.mapArray(MyModel.self, params: { () -> (ModelableParameterType) in
return CustomParameter()
}) else {return}
*/
}Rx// let rxRequest: Single<Response>
// MARK: Rx
let rxRequest = lxfNetTool.rx.request(.data(type: .all, size: 10, index: 1))
// Models
rxRequest.mapArray(MyModel.self).subscribe(onSuccess: { models in
for model in models {
print("id -- \(model._id)")
}
}).disposed(by: dispseBag)
// Models + Result
rxRequest.mapArrResult(MyModel.self).subscribe(onSuccess: { (result, models) in
print("isSuccess --\(result.0)")
print("tipStr --\(result.1)")
print("models count -- \(models.count)")
}).disposed(by: dispseBag)
// 获取指定路径的值
rxRequest.fetchString(keys: [0, "_id"]).subscribe(onSuccess: { str in
print("str -- \(str)")
}).disposed(by: dispseBag)JSON数据对照为方便理解,这里给出具体使用JSON数据图,结合 Example食用更佳~CocoaPods默认安装MoyaMapper默认只安装Core下的文件pod 'MoyaMapper'RxSwift拓展pod 'MoyaMapper/Rx'LicenseMoyaMapper is available under the MIT license. See the LICENSE file for more info.AuthorLinXunFengemail: [email protected]掘金简书
var successValue: String {
return "false"
}
var statusCodeKey: String {
return "error"
}
var tipStrKey: String {
return ""
}
var modelKey: String {
return "results"
}
}以plugin的方式传递给MoyaProviderlet lxfNetTool = MoyaProvider<LXFNetworkTool>(plugins: [MoyaMapperPlugin(NetParameter())])二、定义模型创建一个继承于Modelable的结构体struct MyModel: Modelable {
var _id = ""
......
init?(_ json: JSON) {
}
mutating func mapping(_ json: JSON) {
self._id = json["_id"].stringValue
......
}
}三、解析这里只贴出主要代码NormallxfNetTool.request(.data(type: .all, size: 10, index: 1)) { result in
guard let response = result.value else { return }
// Models
guard let models = try? response.mapArray(MyModel.self) else {return}
for model in models {
print("id -- \(model._id)")
}
// 使用自定义模型参数类
/*
guard let models = try? response.mapArray(MyModel.self, params: { () -> (ModelableParameterType) in
return CustomParameter()
}) else {return}
*/
}Rx// let rxRequest: Single<Response>
// MARK: Rx
let rxRequest = lxfNetTool.rx.request(.data(type: .all, size: 10, index: 1))
// Models
rxRequest.mapArray(MyModel.self).subscribe(onSuccess: { models in
for model in models {
print("id -- \(model._id)")
}
}).disposed(by: dispseBag)
// Models + Result
rxRequest.mapArrResult(MyModel.self).subscribe(onSuccess: { (result, models) in
print("isSuccess --\(result.0)")
print("tipStr --\(result.1)")
print("models count -- \(models.count)")
}).disposed(by: dispseBag)
// 获取指定路径的值
rxRequest.fetchString(keys: [0, "_id"]).subscribe(onSuccess: { str in
print("str -- \(str)")
}).disposed(by: dispseBag)JSON数据对照为方便理解,这里给出具体使用JSON数据图,结合 Example食用更佳~CocoaPods默认安装MoyaMapper默认只安装Core下的文件pod 'MoyaMapper'RxSwift拓展pod 'MoyaMapper/Rx'LicenseMoyaMapper is available under the MIT license. See the LICENSE file for more info.AuthorLinXunFengemail: [email protected]掘金简书