Const.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // Const.swift
  3. // swift翻译
  4. //
  5. // Created by zhongyuan on 2018/3/20.
  6. // Copyright © 2018年 zhongyuan. All rights reserved.
  7. //
  8. import UIKit
  9. /// kScreenWidth
  10. let kScreenWidth = UIScreen.main.bounds.size.width
  11. /// kScreenHeight
  12. let kScreenHeight = UIScreen.main.bounds.size.height
  13. /// 应用程序名称
  14. let app_Name = Bundle.main.infoDictionary!["CFBundleDisplayName"] as! String
  15. /// 系统版本号
  16. let app_Version = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
  17. /// bundle version
  18. let bundle_Version = Bundle.main.infoDictionary!["CFBundleVersion"] as! String
  19. // MARK: - 颜色相关
  20. /// Color_Quote_Up
  21. let Color_Quote_Up = UIColor.fromHex(rgbValue: 0xff1313)
  22. /// Color_Quote_Down
  23. let Color_Quote_Down = UIColor.fromHex(rgbValue: 0x00c280)
  24. /// Color_Quote_Normal
  25. let Color_Quote_Normal = UIColor.hex333()
  26. /// 输入框 placeholder text color
  27. var Color_placeholder_text = UIColor.fromHex(rgbValue: 0x83B7FF)
  28. // MARK: - 数组去重方法
  29. extension Array {
  30. /// 该函数的参数filterCall是一个带返回值的闭包,传入模型T,返回一个E类型
  31. func handleFilter<E: Equatable>(_ filterCall: (Array.Element) -> E) -> [Array.Element] {
  32. var temp = [Array.Element]()
  33. for model in self {
  34. /// 调用filterCall,获得需要用来判断的属性E
  35. let identifer = filterCall(model)
  36. /// 此处利用map函数 来将model类型数组转换成E类型的数组,以此来判断identifer 是否已经存在,如不存在则将model添加进temp
  37. if !temp.compactMap( { filterCall($0) } ).contains(identifer) {
  38. temp.append(model)
  39. }
  40. }
  41. return temp
  42. }
  43. /// 删除指定下标组元素的方法
  44. ///
  45. /// - Parameter indexes: 指定下标组
  46. mutating func removeAtIndexes(indexes: IndexSet) {
  47. let indexes = indexes.sorted(by: { $0 > $1 })
  48. for i in indexes {
  49. self.remove(at: i)
  50. }
  51. }
  52. /// 根据元素去重
  53. /// - Parameter filter:
  54. func filterDuplicates<E: Equatable>(_ filter: (Element) -> E) -> [Element] {
  55. var result = [Element]()
  56. for value in self {
  57. let key = filter(value)
  58. if !result.map({filter($0)}).contains(key) {
  59. result.append(value)
  60. }
  61. }
  62. return result
  63. }
  64. }
  65. extension Array where Element == (key: String, data: Any) {
  66. mutating func remove(key: String) {
  67. if let index = self.firstIndex(where: { $0.key == key }) {
  68. self.remove(at: index)
  69. }
  70. }
  71. /// 可先将原有key的元素删除再追加目标元素的方法
  72. ///
  73. /// - Parameters:
  74. /// - newElement: 目标元素
  75. /// - isOverlay: 是否先删除原有
  76. mutating func append(newElement: (key: String, data: Any), isOverlay: Bool = true) {
  77. self.remove(key: newElement.key)
  78. self.append(newElement)
  79. }
  80. }
  81. extension NSMutableArray {
  82. /// remove
  83. /// - Parameter key: key
  84. func remove(key: String) {
  85. if let object = self.first(where: { ($0 as! (key: String, data: Any)).key == key }) {
  86. self.remove(object)
  87. }
  88. }
  89. /// 可先将原有key的元素删除再追加目标元素的方法
  90. ///
  91. /// - Parameters:
  92. /// - newElement: 目标元素
  93. /// - isOverlay: 是否先删除原有
  94. func append(newElement: (key: String, data: Any), isOverlay: Bool = true) {
  95. self.remove(key: newElement.key)
  96. self.add(newElement)
  97. }
  98. }
  99. /// swift字典转模型
  100. ///
  101. /// - Parameters:
  102. /// - type: 模型类
  103. /// - KeyAndValues: 字典
  104. /// - Returns: 模型对象
  105. /// - Throws: 转换失败 && 解码失败
  106. func jsonModel<T>(_ type: T.Type, with KeyAndValues: [String: Any]) -> T? where T: Decodable {
  107. let jsonObject = try? JSONSerialization.data(withJSONObject: KeyAndValues, options: [])
  108. return try? JSONDecoder().decode(type, from: jsonObject!)
  109. }
  110. extension NSLayoutConstraint {
  111. /**
  112. Change multiplier constraint
  113. - parameter multiplier: CGFloat
  114. - returns: NSLayoutConstraint
  115. */
  116. func setMultiplier(multiplier: CGFloat) -> NSLayoutConstraint {
  117. NSLayoutConstraint.deactivate([self])
  118. let newConstraint = NSLayoutConstraint(
  119. item: firstItem!,
  120. attribute: firstAttribute,
  121. relatedBy: relation,
  122. toItem: secondItem,
  123. attribute: secondAttribute,
  124. multiplier: multiplier,
  125. constant: self.constant)
  126. newConstraint.priority = priority
  127. newConstraint.shouldBeArchived = self.shouldBeArchived
  128. newConstraint.identifier = self.identifier
  129. NSLayoutConstraint.activate([newConstraint])
  130. return newConstraint
  131. }
  132. }
  133. extension Int {
  134. /// 数字专中文
  135. /// - Returns: String
  136. func intIntoString() -> String {
  137. let formatter = NumberFormatter()
  138. formatter.numberStyle = NumberFormatter.Style(rawValue: UInt(CFNumberFormatterRoundingMode.roundHalfDown.rawValue))!
  139. let string: String = formatter.string(from: NSNumber(value: self))!
  140. return string
  141. }
  142. }
  143. extension String {
  144. /// 判断是否数字的方法
  145. ///
  146. /// - Returns: 判断是否数字
  147. func isFloatNumber() -> Bool {
  148. let rule = "^(-?\\d+)(\\.\\d+)?$"
  149. let pred = NSPredicate(format:"SELF MATCHES %@", rule)
  150. let isMatch = pred.evaluate(with: self)
  151. return isMatch
  152. }
  153. /// Create `Data` from hexadecimal string representation
  154. ///
  155. /// 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.
  156. ///
  157. /// - returns: Data represented by this hexadecimal string.
  158. func hexadecimal() -> Data? {
  159. var data = Data(capacity: self.count / 2)
  160. let regex = try! NSRegularExpression(pattern: "[0-9a-f]{1,2}", options: .caseInsensitive)
  161. regex.enumerateMatches(in: self, range: NSMakeRange(0, utf16.count)) { match, flags, stop in
  162. let byteString = (self as NSString).substring(with: match!.range)
  163. var num = UInt8(byteString, radix: 16)!
  164. data.append(&num, count: 1)
  165. }
  166. guard data.count > 0 else { return nil }
  167. return data
  168. }
  169. /// 获取UrlString
  170. /// - Returns: String
  171. func getUrlString() -> String {
  172. return self.replacingOccurrences(of: "./uploadFile", with: MTP2BusinessCore.getUploadUrl()+"/uploadFile")
  173. }
  174. }