项目作者: psvmc

项目描述 :
这是一个把 Alamofire 请求的数据 转成 ObjectMapper 形式的对象的组件
高级语言: Swift
项目地址: git://github.com/psvmc/RxSwift-ObjectMapper.git
创建时间: 2016-04-22T01:39:02Z
项目社区:https://github.com/psvmc/RxSwift-ObjectMapper

开源协议:MIT License

下载


RxSwift-ObjectMapper

这是一个把 Alamofire 请求的数据 转成 ObjectMapper 形式的 对象

安装方法(Installation)

你可以通过CocoaPods 来使用RxSwift-ObjectMapper 在你的Podfile中添加如下配置

RxSwift-ObjectMapper is available through CocoaPods. To install it, simply add the following line to your Podfile:

  1. pod 'RxSwift-ObjectMapper', '~> 1.1'

然后运行pod install

使用方法(Usage)

创建一个实现 Mappable 协议的Class 或者 Struct

Create a Class or Struct which implements the Mappable protocol.

如果你返回数据的格式为:

if you get the json like this:

  1. {
  2. "success":true,
  3. "msg":"获取用户信息成功",
  4. "obj":[]
  5. }

你可以建立以下两个struct:

you can create two struct like this:

  1. import Foundation
  2. import ObjectMapper
  3. struct ZJResult<T: Mappable>: Mappable {
  4. var success: Bool!
  5. var msg: String!
  6. var obj: [T]?
  7. init?(map: Map) {
  8. }
  9. // Mappable
  10. mutating func mapping(map: Map) {
  11. success <- map["success"]
  12. msg <- map["msg"]
  13. obj <- map["obj"]
  14. }
  15. }
  1. import Foundation
  2. import ObjectMapper
  3. struct ZJArticle: Mappable {
  4. var title: String!
  5. var keywords: String!
  6. var description: String!
  7. var date: String!
  8. var path: String!
  9. var url: String!
  10. init?(map: Map) {
  11. }
  12. // Mappable
  13. mutating func mapping(map: Map) {
  14. title <- map["title"]
  15. keywords <- map["keywords"]
  16. description <- map["description"]
  17. date <- map["date"]
  18. path <- map["path"]
  19. url <- map["url"]
  20. }
  21. }

添加pod库

add pod

  1. pod 'RxAlamofire'

然后我们就可以这样请求数据了

then we can query data like this:

  1. _ = string(.get,
  2. "http://www.psvmc.cn/navi_list.json",
  3. parameters: ["userPhone":"15225178360","userLoginPswd":"123456"])
  4. .observeOn(MainScheduler.instance)
  5. .mapObject(type: ZJResult<ZJArticle>.self)
  6. .subscribe(
  7. onNext: { repos -> Void in
  8. self.showTextView.text = "用ObjectMapper把结果转为对象\n"
  9. + "是否成功:\(repos.success!)\n"
  10. + "信息:\(repos.msg!)";
  11. },
  12. onError: { (error) -> Void in
  13. self.showTextView.text = "\(error)";
  14. })

是不是很简单

so easy

作者(Author)

剑行者

版权(License)

你可以在MIT许可下使用RxSwift-ObjectMapper,更多信息请查看LICENSE文件

RxSwift-ObjectMapper is available under the MIT license. See the LICENSE file for more info.