Latest 1.1
Homepage https://github.com/psvmc/RxSwift-ObjectMapper
License MIT
Platforms ios 8.0, requires ARC
Dependencies RxSwift, ObjectMapper
Frameworks Foundation
Authors

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:

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:

{
    "success":true,
    "msg":"获取用户信息成功",
    "obj":[]
}

你可以建立以下两个struct:

you can create two struct like this:

import Foundation
import ObjectMapper
struct ZJResult<T: Mappable>: Mappable {
    var success: Bool!
    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 ZJArticle: Mappable {
    var title: String!
    var keywords: String!
    var description: String!
    var date: String!
    var path: String!
    var url: String!

    init?(map: Map) {

    }

    // Mappable
    mutating func mapping(map: Map) {
        title    <- map["title"]
        keywords   <- map["keywords"]
        description   <- map["description"]
        date   <- map["date"]
        path   <- map["path"]
        url   <- map["url"]
    }
}

添加pod库

add pod

pod 'RxAlamofire'

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

then we can query data like this:

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

是不是很简单

so easy

作者(Author)

剑行者

版权(License)

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

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

Latest podspec

{
    "name": "RxSwift-ObjectMapper",
    "version": "1.1",
    "summary": "ObjectMapper bindings for RxSwift",
    "description": "Easy to use ObjectMapper in RxSwift",
    "homepage": "https://github.com/psvmc/RxSwift-ObjectMapper",
    "license": {
        "type": "MIT",
        "file": "License"
    },
    "authors": {
        "psvmc": "[email protected]"
    },
    "source": {
        "git": "https://github.com/psvmc/RxSwift-ObjectMapper.git",
        "tag": "1.1"
    },
    "pod_target_xcconfig": {
        "SWIFT_VERSION": "3.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": "true",
    "source_files": "RxAlamofireDemo_Swift/Lib/Lib-Swift/RxSwift-ObjectMapper/*.swift",
    "dependencies": {
        "RxSwift": [
            "~> 3.0"
        ],
        "ObjectMapper": [
            "~> 2.2"
        ]
    },
    "frameworks": "Foundation"
}

Pin It on Pinterest

Share This