| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- //
- // Const.swift
- // swift翻译
- //
- // Created by zhongyuan on 2018/3/20.
- // Copyright © 2018年 zhongyuan. All rights reserved.
- //
- import UIKit
- /// kScreenWidth
- let kScreenWidth = UIScreen.main.bounds.size.width
- /// kScreenHeight
- let kScreenHeight = UIScreen.main.bounds.size.height
- /// 应用程序名称
- let app_Name = Bundle.main.infoDictionary!["CFBundleDisplayName"] as! String
- /// 系统版本号
- let app_Version = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
- /// bundle version
- let bundle_Version = Bundle.main.infoDictionary!["CFBundleVersion"] as! String
- // MARK: - 颜色相关
- /// Color_Quote_Up
- let Color_Quote_Up = UIColor.fromHex(rgbValue: 0xff1313)
- /// Color_Quote_Down
- let Color_Quote_Down = UIColor.fromHex(rgbValue: 0x00c280)
- /// Color_Quote_Normal
- let Color_Quote_Normal = UIColor.hex333()
- /// 输入框 placeholder text color
- var Color_placeholder_text = UIColor.fromHex(rgbValue: 0x83B7FF)
- // MARK: - 数组去重方法
- extension Array {
- /// 该函数的参数filterCall是一个带返回值的闭包,传入模型T,返回一个E类型
- func handleFilter<E: Equatable>(_ filterCall: (Array.Element) -> E) -> [Array.Element] {
- var temp = [Array.Element]()
- for model in self {
- /// 调用filterCall,获得需要用来判断的属性E
- let identifer = filterCall(model)
- /// 此处利用map函数 来将model类型数组转换成E类型的数组,以此来判断identifer 是否已经存在,如不存在则将model添加进temp
- if !temp.compactMap( { filterCall($0) } ).contains(identifer) {
- temp.append(model)
- }
- }
- return temp
- }
-
- /// 删除指定下标组元素的方法
- ///
- /// - Parameter indexes: 指定下标组
- mutating func removeAtIndexes(indexes: IndexSet) {
- let indexes = indexes.sorted(by: { $0 > $1 })
- for i in indexes {
- self.remove(at: i)
- }
- }
-
- /// 根据元素去重
- /// - Parameter filter:
- func filterDuplicates<E: Equatable>(_ filter: (Element) -> E) -> [Element] {
- var result = [Element]()
- for value in self {
- let key = filter(value)
- if !result.map({filter($0)}).contains(key) {
- result.append(value)
- }
- }
- return result
- }
- }
- extension Array where Element == (key: String, data: Any) {
- mutating func remove(key: String) {
- if let index = self.firstIndex(where: { $0.key == key }) {
- self.remove(at: index)
- }
- }
-
- /// 可先将原有key的元素删除再追加目标元素的方法
- ///
- /// - Parameters:
- /// - newElement: 目标元素
- /// - isOverlay: 是否先删除原有
- mutating func append(newElement: (key: String, data: Any), isOverlay: Bool = true) {
- self.remove(key: newElement.key)
- self.append(newElement)
- }
- }
- extension NSMutableArray {
- /// remove
- /// - Parameter key: key
- func remove(key: String) {
- if let object = self.first(where: { ($0 as! (key: String, data: Any)).key == key }) {
- self.remove(object)
- }
- }
-
- /// 可先将原有key的元素删除再追加目标元素的方法
- ///
- /// - Parameters:
- /// - newElement: 目标元素
- /// - isOverlay: 是否先删除原有
- func append(newElement: (key: String, data: Any), isOverlay: Bool = true) {
- self.remove(key: newElement.key)
- self.add(newElement)
- }
- }
- /// swift字典转模型
- ///
- /// - Parameters:
- /// - type: 模型类
- /// - KeyAndValues: 字典
- /// - Returns: 模型对象
- /// - Throws: 转换失败 && 解码失败
- func jsonModel<T>(_ type: T.Type, with KeyAndValues: [String: Any]) -> T? where T: Decodable {
- let jsonObject = try? JSONSerialization.data(withJSONObject: KeyAndValues, options: [])
- return try? JSONDecoder().decode(type, from: jsonObject!)
- }
- extension NSLayoutConstraint {
- /**
- Change multiplier constraint
- - parameter multiplier: CGFloat
- - returns: NSLayoutConstraint
- */
- func setMultiplier(multiplier: CGFloat) -> NSLayoutConstraint {
-
- NSLayoutConstraint.deactivate([self])
-
- let newConstraint = NSLayoutConstraint(
- item: firstItem!,
- attribute: firstAttribute,
- relatedBy: relation,
- toItem: secondItem,
- attribute: secondAttribute,
- multiplier: multiplier,
- constant: self.constant)
- newConstraint.priority = priority
- newConstraint.shouldBeArchived = self.shouldBeArchived
- newConstraint.identifier = self.identifier
- NSLayoutConstraint.activate([newConstraint])
- return newConstraint
- }
- }
- extension Int {
- /// 数字专中文
- /// - Returns: String
- func intIntoString() -> String {
- let formatter = NumberFormatter()
- formatter.numberStyle = NumberFormatter.Style(rawValue: UInt(CFNumberFormatterRoundingMode.roundHalfDown.rawValue))!
- let string: String = formatter.string(from: NSNumber(value: self))!
- return string
- }
- }
- extension String {
- /// 判断是否数字的方法
- ///
- /// - Returns: 判断是否数字
- func isFloatNumber() -> Bool {
- let rule = "^(-?\\d+)(\\.\\d+)?$"
- let pred = NSPredicate(format:"SELF MATCHES %@", rule)
- let isMatch = pred.evaluate(with: self)
- return isMatch
- }
-
- /// Create `Data` from hexadecimal string representation
- ///
- /// This takes a hexadecimal representation and creates a `Data` object. Note, if the string has any spaces or non-hex characters (e.g. starts with '<' and with a '>'), those are ignored and only hex characters are processed.
- ///
- /// - returns: Data represented by this hexadecimal string.
- func hexadecimal() -> Data? {
- var data = Data(capacity: self.count / 2)
- let regex = try! NSRegularExpression(pattern: "[0-9a-f]{1,2}", options: .caseInsensitive)
- regex.enumerateMatches(in: self, range: NSMakeRange(0, utf16.count)) { match, flags, stop in
- let byteString = (self as NSString).substring(with: match!.range)
- var num = UInt8(byteString, radix: 16)!
- data.append(&num, count: 1)
- }
- guard data.count > 0 else { return nil }
- return data
- }
-
- /// 获取UrlString
- /// - Returns: String
- func getUrlString() -> String {
- return self.replacingOccurrences(of: "./uploadFile", with: MTP2BusinessCore.getUploadUrl()+"/uploadFile")
- }
- }
|