测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最新发布 | 2016年10月 |
SPM支持SPM | ✗ |
由 psvmc 维护。
依赖 | |
RxSwift | ~> 3.0 |
ObjectMapper | ~> 2.2 |
这是一个将Alamofire请求数据转换为ObjectMapper形式的对象
您可以通过CocoaPods使用RxSwift-ObjectMapper。在Podfile中添加以下配置
RxSwift-ObjectMapper可通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile
pod 'RxSwift-ObjectMapper', '~> 1.0'
然后运行pod install
创建一个实现Mappable协议的Class或Struct
Create a Class or Struct which implements the Mappable protocol.
如果返回的数据格式为:
if you get the json like this
{
"success":"true",
"msg":"获取用户信息成功",
"obj":{
"userName":"张剑",
"userAlias":"剑行者",
"invitationCode":100
}
}
你可以建立以下两个struct:
you can create two struct like this
import Foundation
import ObjectMapper
struct ZJResult<T: Mappable>: Mappable {
var success: String!
var msg: String!
var obj: T?
init?(_ map: Map) {
}
// Mappable
mutating func mapping(map: Map) {
success <- map["success"]
msg <- map["msg"]
obj <- map["obj"]
}
}
import Foundation
import ObjectMapper
struct ZJUser: Mappable {
var userName: String!
var userPhone: String!
var userAlias: String!
var invitationCode: Int!
init?(_ map: Map) {
}
// Mappable
mutating func mapping(map: Map) {
userName <- map["userName"]
userPhone <- map["userPhone"]
userAlias <- map["userAlias"]
invitationCode <- map["invitationCode"]
}
}
添加pod库
add pod
pod 'RxAlamofire'
然后我们就可以这样请求数据了
then we can query data like this
_ = string(.POST,
"http://t.yidaisong.com:90/login!in.do",
parameters: ["userPhone":"15225178360","userLoginPswd":"123456"])
.observeOn(MainScheduler.instance)
.mapObject(ZJResult<ZJUser>)
.subscribe(
onNext: { repos -> Void in
self.showTextView.text = "用ObjectMapper把结果转为对象\n"
+ "用户名:\(repos.obj!.userName)\n"
+ "昵称:\(repos.obj!.userAlias)";
},
onError: { (error) -> Void in
self.showTextView.text = "\(error)";
})
是不是很简单
so easy
使用方法可以参考以下示例
Method of use can refer to the following example
剑行者
您可以在MIT许可下使用RxSwift-ObjectMapper,更多信息请查看LICENSE文件
RxSwift-ObjectMapper可在MIT许可下使用。有关更多信息,请参阅LICENSE文件。