Swift 常用的工具类

1.网络请求工具类

  • 使用Alamofire
//  网络工具类

import Foundation
import Alamofire

class AlamofireTools: NSObject {
    
    
    /// 错误结构体
    struct errorResult: Error {
        
    }
    
    
    /// GET方法
    ///
    /// - Parameters:
    ///   - url: 网址
    ///   - parameters: 参数
    ///   - success: 成功回调
    ///   - fail: 失败回调
    static func getAlamofireData(url: String, parameters: Parameters? = nil, success: @escaping (_ json: [String: Any]) ->(), fail: @escaping (_ error: Error) ->()) {

        let domainUrl = DOMAIMURL + url
        
        Alamofire.request(domainUrl, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
            if response.result.error != nil { // 错误
                guard let responseError = response.result.error else {
                    // 失败返回
                    fail(errorResult())
                    return
                }
                
                // 失败返回
                fail(responseError)
                
                
            }else { // 成功
                
                // 成功返回
                let responseValue = response.result.value ?? [String: Any]()
                success(responseValue as! [String : Any])
            }
        }
    }
    
    /// POST请求
    ///
    /// - Parameters:
    ///   - url: 网址
    ///   - parameters: 参数
    ///   - success: 成功回调
    ///   - fail: 失败回调
    static func postAlamofireData(url: String, parameters: Parameters? = nil, success: @escaping (_ json: [String: Any]) ->(), fail: @escaping (_ error: Error) ->()) {
        let domainUrl = DOMAIMURL + url
        
        Alamofire.request(domainUrl, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
            if response.result.error != nil { // 错误
                guard let responseError = response.result.error else {
                    // 失败返回
                    fail(errorResult())
                    return
                }
                
                // 失败返回
                fail(responseError)
                
                
            }else { // 成功
                
                // 成功返回
                let responseValue = response.result.value ?? [String: Any]()
                success(responseValue as! [String : Any])
            }
        }
    }
}

2.颜色工具类

extension UIColor { // 颜色

    /// 颜色
    ///
    /// - Parameters:
    ///   - red: 红色值(0 -- 255)
    ///   - blue: 蓝色值(0 -- 255)
    ///   - green: 绿色值(0 -- 255)
    ///   - alpha: 透明度(0 -- 1)
    static func rgba(red: CGFloat, blue: CGFloat, green: CGFloat, alpha: CGFloat = 1.0) -> UIColor {
        return UIColor.init(red: red / 255.0, green: green / 255.0, blue: blue / 255.0, alpha: alpha)
    }
    
    /// 随机颜色
    static func randomColor() -> UIColor {
        let red = CGFloat(arc4random_uniform(256))
        let green = CGFloat(arc4random_uniform(256))
        let blue = CGFloat(arc4random_uniform(256))
        return UIColor.rgba(red: red, blue: green, green: blue)
    }
    
    /// 16进制颜色
    ///
    /// - Parameter hex: 16进制颜色
    /// - Parameter alpha: 透明度
    static func hexColor(hex: String, alpha: CGFloat = 1.0) -> UIColor {
        var red:   CGFloat = 0.0
        var green: CGFloat = 0.0
        var blue:  CGFloat = 0.0
        var alpha: CGFloat = alpha
        var hex = hex
        
        if hex.hasPrefix("#") {
            let index = hex.index(hex.startIndex, offsetBy: 1)
            hex = String(hex[index...])
            
        }
        
        let scanner = Scanner(string: hex)
        var hexValue: CUnsignedLongLong = 0
        if scanner.scanHexInt64(&hexValue) {
            switch (hex.count) {
            case 3:
                red   = CGFloat((hexValue & 0xF00) >> 8)       / 15.0
                green = CGFloat((hexValue & 0x0F0) >> 4)       / 15.0
                blue  = CGFloat(hexValue & 0x00F)              / 15.0
            case 4:
                red   = CGFloat((hexValue & 0xF000) >> 12)     / 15.0
                green = CGFloat((hexValue & 0x0F00) >> 8)      / 15.0
                blue  = CGFloat((hexValue & 0x00F0) >> 4)      / 15.0
                alpha = CGFloat(hexValue & 0x000F)             / 15.0
            case 6:
                red   = CGFloat((hexValue & 0xFF0000) >> 16)   / 255.0
                green = CGFloat((hexValue & 0x00FF00) >> 8)    / 255.0
                blue  = CGFloat(hexValue & 0x0000FF)           / 255.0
            case 8:
                red   = CGFloat((hexValue & 0xFF000000) >> 24) / 255.0
                green = CGFloat((hexValue & 0x00FF0000) >> 16) / 255.0
                blue  = CGFloat((hexValue & 0x0000FF00) >> 8)  / 255.0
                alpha = CGFloat(hexValue & 0x000000FF)         / 255.0
            default:
                print("Invalid RGB string, number of characters after '#' should be either 3, 4, 6 or 8", terminator: "")
            }
        } else {
            print("Scan hex error")
        }
       return UIColor.init(red:red, green:green, blue:blue, alpha:alpha)
    }
}

你可能感兴趣的:(Swift 常用的工具类)