Swift封装利器 - 协议扩展

import UIKit

/// 新建一个协议
public protocol BaseTypeAble {
    /// 关联类型
    associatedtype WarpperType;
    var type: WarpperType { get };
}

/// 定义一个类
public final class ExtensionBaseTypeAble: BaseTypeAble {
    /// 泛型
    public let type: T;
    /// 构造方法
    public init(_ type: T) {
        self.type = type;
    }
}


/// 为协议实现默认方法
extension BaseTypeAble where WarpperType == UIColor{
    /// 获取红色
    func red() -> CGFloat {
        var value: CGFloat = 0
        type.getRed(&value, green: nil, blue: nil, alpha: nil)
        return value
    }
    /// 获取绿色
    func green() -> CGFloat {
        var value: CGFloat = 0
        type.getRed(nil, green: &value, blue: nil, alpha: nil)
        return value
    }
    /// 获取蓝色
    func blue() -> CGFloat {
        var value: CGFloat = 0
        type.getRed(nil, green: nil, blue: &value, alpha: nil)
        return value
    }
}

extension UIColor{
    var type: ExtensionBaseTypeAble {
        return ExtensionBaseTypeAble(self);
    }
    
}

extension BaseTypeAble where WarpperType == Data{
    
    func toUInt8() -> [UInt8] {
        var bytes = [UInt8](repeatElement(0, count: type.count));
        type.copyBytes(to: &bytes, count: type.count);
        return bytes;
    }
    
    func toHex() -> String {
        var hex: String = "";
        for i in 0.. {
        return ExtensionBaseTypeAble(self);
    }
    
}

Extension+Protocol.swift

你可能感兴趣的:(Swift封装利器 - 协议扩展)