码迷,mamicode.com
首页 > 移动开发 > 详细

RxSwift + Moya + ObjectMapper

时间:2019-01-14 18:56:22      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:sel   val   swift   amp   param   sub   uikit   work   tps   

 

 

https://www.jianshu.com/p/173915b943af

 

use_frameworks!

target ‘RXDemo‘ do
    pod ‘RxSwift‘
    pod ‘RxCocoa‘
    pod ‘Moya-ObjectMapper/RxSwift‘
    pod ‘Moya/RxSwift‘
end

  

import Moya

let DouBanProvider = MoyaProvider<DouBanAPI>()

public enum DouBanAPI {
    case channels  //获取频道列表
    case playlist(String) //获取歌曲
}

extension DouBanAPI: TargetType {
 
    public var baseURL: URL {
        switch self {
        case .channels:
            return URL(string: "https://www.douban.com")!
        case .playlist(_):
            return URL(string: "https://douban.fm")!
        }
    }
    
    public var path: String {
        switch self {
        case .channels:
            return "/j/app/radio/channels"
        case .playlist(_):
            return "/j/mine/playlist"
        }
    }
    
    public var method: Moya.Method {
        return .get
    }

    public var task: Task {
        switch self {
        case .playlist(let channel):
            var params: [String: Any] = [:]
            params["channel"] = channel
            params["type"] = "n"
            params["from"] = "mainsite"
            return .requestParameters(parameters: params,
                                      encoding: URLEncoding.default)
        default:
            return .requestPlain
        }
    }
    
    public var validate: Bool {
        return false
    }
    
    public var sampleData: Data {
        return "{}".data(using: String.Encoding.utf8)!
    }
    
    public var headers: [String: String]? {
        return nil
    }
}

 

import UIKit
import ObjectMapper

//豆瓣接口模型
struct Douban: Mappable {
    //频道列表
    var channels: [Channel]?
    
    init?(map: Map) { }
    
    // Mappable
    mutating func mapping(map: Map) {
        channels <- map["channels"]
    }
}

//频道模型
struct Channel: Mappable {
    var name: String?
    var nameEn:String?
    var channelId: String?
    var seqId: Int?
    var abbrEn: String?
    
    init?(map: Map) { }
    
    // Mappable
    mutating func mapping(map: Map) {
        name <- map["name"]
        nameEn <- map["name_en"]
        channelId <- map["channel_id"]
        seqId <- map["seq_id"]
        abbrEn <- map["abbr_en"]
    }
}

//歌曲列表模型
struct Playlist: Mappable {
    var r: Int!
    var isShowQuickStart: Int!
    var song:[Song]!
    
    init?(map: Map) { }
    
    // Mappable
    mutating func mapping(map: Map) {
        r <- map["r"]
        isShowQuickStart <- map["is_show_quick_start"]
        song <- map["song"]
    }
}

//歌曲模型
struct Song: Mappable {
    var title: String!
    var artist: String!
    
    init?(map: Map) { }
    
    // Mappable
    mutating func mapping(map: Map) {
        title <- map["title"]
        artist <- map["artist"]
    }
}

  

  DouBanProvider.rx.request(.channels)
            .mapObject(Douban.self)
            .subscribe(onSuccess: { douban in
                if let channels = douban.channels {
                    print("--- 共\(channels.count)个频道 ---")
                    for channel in channels {
                        if let name = channel.name, let channelId = channel.channelId {
                            print("\(name) (id:\(channelId))")
                        }
                    }
                }
            }, onError: { error in
                print("数据请求失败!错误原因:", error)
            })
            .disposed(by: disposeBag)

  

  

 

RxSwift + Moya + ObjectMapper

标签:sel   val   swift   amp   param   sub   uikit   work   tps   

原文地址:https://www.cnblogs.com/youhui/p/10268251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!