【Swift】项目梳理

Part1 网络请求部分

网络层这一块用Alamofire+Moya,如同于在OC中用AFNetworking+YTKNetwork

xxxApiService.swift —> 负责定义接口枚举、参数声明
import Foundation
import Moya

enum MineService {
    //查询体验金
    case checkExperMoney
    //体验金列表
    case experienceMoneyList(pageNo: Int, status: Int)
}

extension MineService: TargetType {
    
    /// The path to be appended to `baseURL` to form the full `URL`.
    var path: String {
        
        switch self {

        case .checkExperMoney:
            return "/v1/mine/experience.json"
            
        case .experienceMoneyList:
            return "/v1/mine/experienceMoney.json"
        }
    }
    
    /// The parameters to be encoded in the request.
    var parameters: [String: Any]? {
        
        switch self {
            
        case .experienceMoneyList(let pageNo,  let status):
            return ["pageNo": pageNo, "pageSize": 10, "status": status]
            
        case .checkExperMoney:
            return nil
        }
    }
}

xxxViewModel.swift -> 负责发送请求、指定返回类型
import UIKit
import PromiseKit
import RxSwift

class MineVModel: BaseViewModel {
    
    func experienceMoneyList(pageNo: Int, status: Int) -> Promise<[ExperMoneyItem]> {
        
        return Promise(resolvers: { (resolve, reject) in
            
            self.sendRequest(target: MineService.experienceMoneyList(pageNo: pageNo, status: status))
                .mapModelArray(ExperMoneyItem.self)
                .subscribe(onNext: {
                    resolve($0)
                }, onError: {
                    reject($0)
                })
                .addDisposableTo(self.disposeBag)
        })
    }

    func checkExperMoney() -> Promise {
        
        return Promise(resolvers: { (resolve, reject) in
            
            self.sendRequest(target: MineService.checkExperMoney)
                .mapModelObject(ExperMoneyData.self)
                .subscribe(onNext: {
                    resolve($0)
                }, onError: {
                    reject($0)
                })
                .addDisposableTo(self.disposeBag)
            
        })
    }
}

请求返回值目前支持3种解析方式:

1、Object:指定model进行解析
func registerRequest(telephone:String, password:String, smsCode:String) -> Promise {
        
        return Promise(resolvers: { (resolve, reject) in
            
            self.sendRequest(target: AuthApiService.registerRequest(telephone: telephone, password: password, smsCode:smsCode), withHUD: true)
                .mapModelObject(AuthModel.self)
                .subscribe(onNext: {
                    resolve($0)
                }, onError: {
                    reject($0)
                })
                .addDisposableTo(self.disposeBag)
        })
}
2、Array:指定数组进行解析
func purchaseDetailList(pageNo: Int) -> Promise<[PurchaseDetail]> {
        
        return Promise(resolvers: { (resolve, reject) in
            
            self.sendRequest(target: MineService.purchaseDetailList(pageNo: pageNo))
            .mapModelArray(PurchaseDetail.self)
            .subscribe(onNext: {
                resolve($0)
            }, onError: {
                reject($0)
            })
            .addDisposableTo(self.disposeBag)
            
        })
}
3、Dictionary:指定字典进行解析
    func getSmsCodeRequest(telephone:String, smsType:SmsType) -> Promise<[String:Any]> {
        
        return Promise(resolvers: { (resolve, reject) in
            
            self.sendRequest(target: AuthApiService.getSmsCodeRequest(telephone: telephone, smsType: smsType.rawValue), withHUD: true)
                .mapModelDictionary()
                .subscribe(onNext: {
                    resolve($0)
                }, onError: {
                    reject($0)
                })
                .addDisposableTo(self.disposeBag)
        })
}

Part2 常用第三方库

1、R.Swift
主要用于建立资源文件的映射,方便快速联想到对应image、nib等文件

var appGuideView   = R.nib.guideView.firstView(owner: nil)!
self.banksTableView.register(R.nib.backSelectionCell)
let cell = tableView.dequeueReusableCell(withIdentifier: R.nib.backSelectionCell.identifier) as! BackSelectionCell

rightBtn.setImage(UIImage(named: R.image.my_money_pot_detailed.name), for: .normal)
var msgBtnImage  = R.image.home_nav_news()

参考地址:https://github.com/mac-cain13/R.swift
2、MonkeyKing
第三方分享 详见ShareView

参考地址:https://github.com/nixzhu/MonkeyKing

你可能感兴趣的:(【Swift】项目梳理)