美文网首页swiftiOS 底层面试
Alamofire(13)——POP面向协议编程、Moya

Alamofire(13)——POP面向协议编程、Moya

作者: 无悔zero | 来源:发表于2020-12-29 06:34 被阅读0次

POP越来越频繁使用了,先说说什么是POPPOP就是面对协议编程。平常的面对对象编程叫OOP

POP简单来说就是,通过协议扩展对象的能力。来看个简单的例子:

  1. 先是有两个类:
class Animation1: NSObject, BirdProtocol {
    func eat() {
        print("吃")
    }
}

class Animation2: NSObject, FishProtocol {
    func eat() {
        print("吃")
    }
}
  1. 通过协议分别扩展两个类的能力,不协议拥有不同的能力:
//鸟的能力 - 协议
protocol BirdProtocol {
    func fly()
}
//鱼的能力 - 协议
protocol FishProtocol {
    func swim()
}
  1. 然后通过extension实现协议的方法:
extension FishProtocol {
    func swim() {
        print("游泳")
    }
}

extension BirdProtocol {
    func fly() {
        print("飞")
    }
}
  1. 这样就让两个类分别拥有了不同的能力:
let animation1 = Animation1()
animation1.fly()

let animation2 = Animation2()
animation2.swim()

OOP其实也可以,创建基类,然后子类继承,但不能继承多个基类。而POP的方式能拥有多个能力,耦合度也更低。

  • POP网络

Swift有个第三方框架叫Moya,相当于OC的YTKNetWorkingYTKNetWorking是对AFNetWorking的封装,Moya是对Alamofire的封装。Moya采用的就是POP协议编程

  1. Moya有个基本协议,这协议把对接口的处理提取出来,减少了逻辑代码的压力,并可以对各个接口做统一处理和分开处理:
public protocol TargetType {

    /// The target's base `URL`.
    var baseURL: URL { get }

    /// The path to be appended to `baseURL` to form the full `URL`.
    var path: String { get }

    /// The HTTP method used in the request.
    var method: Moya.Method { get }

    /// Provides stub data for use in testing.
    var sampleData: Data { get }

    /// The type of HTTP task to be performed.
    var task: Task { get }

    /// The type of validation to perform on the request. Default is `.none`.
    var validationType: ValidationType { get }

    /// The headers to be used in the request.
    var headers: [String: String]? { get }
}
  1. 看个例子:
let provide = MoyaProvider<UserApi>()
provide.request(.login(username, password)) { (result) in
    print(result)
}
public enum UserApi {
    case login(String, String)  //登录接口
    case smscode(String)      //发送验证码
}

extension UserApi: TargetType {
    //服务器地址
    public var baseURL: URL {
        return URL(string:"http://www.xxx.com/")!
    }
    // 各个请求的具体路径
    public var path: String {
        switch self {
        case .login:
            return "login"
        case .smscode:
            return "smscode"
        }
    }
    // 请求方式
    public var method: Moya.Method {
        switch self {
        case .login, .smscode:
            return .post
        default:
            return .get
        }
    }
    //请求任务事件(这里附带上参数)
    public var task: Task {
        var param:[String:Any] = [:]
        switch self {
        case .login(let username,let password):
            param["username"] = username
            param["password"] = password
        case .smscode(let username):
            param["username"] = username
        default:
            return .requestPlain
        }
        return .requestParameters(parameters: param, encoding: URLEncoding.default)
    }
    //设置请求头
    public var headers: [String: String]? {
        return nil
    }
}

相关文章

网友评论

    本文标题:Alamofire(13)——POP面向协议编程、Moya

    本文链接:https://www.haomeiwen.com/subject/wvgxoktx.html