1.網(wǎng)絡(luò)請(qǐng)求工具類
// 網(wǎng)絡(luò)工具類
import Foundation
import Alamofire
class AlamofireTools: NSObject {
/// 錯(cuò)誤結(jié)構(gòu)體
struct errorResult: Error {
}
/// GET方法
///
/// - Parameters:
/// - url: 網(wǎng)址
/// - parameters: 參數(shù)
/// - success: 成功回調(diào)
/// - fail: 失敗回調(diào)
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 { // 錯(cuò)誤
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請(qǐng)求
///
/// - Parameters:
/// - url: 網(wǎng)址
/// - parameters: 參數(shù)
/// - success: 成功回調(diào)
/// - fail: 失敗回調(diào)
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 { // 錯(cuò)誤
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: 藍(lán)色值(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)
}
/// 隨機(jī)顏色
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進(jìn)制顏色
///
/// - Parameter hex: 16進(jìn)制顏色
/// - 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)
}
}