Convert.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Convert.swift
  3. // HCQuanfangtong
  4. //
  5. // Created by Apple on 2021/12/24.
  6. // Copyright © 2021 Jyp. All rights reserved.
  7. //
  8. import Foundation
  9. /// 数据转换
  10. /// 任意类型转 Int,Double, Float, CGFloat, String
  11. class Convert{
  12. static func toInt(_ object: Any?) -> Int?{
  13. if object is Int{
  14. if let middle = object as? Int{
  15. return middle
  16. }
  17. }
  18. else if object is Double{
  19. if let middle = object as? Double{
  20. return Int.init(middle)
  21. }
  22. }
  23. else if object is Float{
  24. if let middle = object as? Float{
  25. return Int.init(middle)
  26. }
  27. }
  28. else if object is String{
  29. if let middle = object as? String{
  30. return Int.init(middle)
  31. }
  32. }
  33. return nil
  34. }
  35. static func toDouble(_ object: Any?) -> Double?{
  36. if object is Double{
  37. if let middle = object as? Double{
  38. return middle
  39. }
  40. }
  41. else if object is Float{
  42. if let middle = object as? Float{
  43. return Double.init(middle)
  44. }
  45. }
  46. else if object is String{
  47. if let middle = object as? String{
  48. return Double.init(middle)
  49. }
  50. }
  51. else if object is Int{
  52. if let middle = object as? Int{
  53. return Double.init(middle)
  54. }
  55. }
  56. return nil
  57. }
  58. static func toFloat(_ object: Any?) -> Float?{
  59. if object is Float{
  60. if let middle = object as? Float{
  61. return middle
  62. }
  63. }
  64. else if object is Double{
  65. if let middle = object as? Double{
  66. return Float.init(middle)
  67. }
  68. }
  69. else if object is Int{
  70. if let middle = object as? Int{
  71. return Float.init(middle)
  72. }
  73. }
  74. else if object is String{
  75. if let middle = object as? String{
  76. return Float.init(middle)
  77. }
  78. }
  79. return nil
  80. }
  81. static func toString(_ object: Any?, decimalNumber: Int? = nil) -> String?{
  82. if object is Int{
  83. if let middle = object as? Int{
  84. if decimalNumber.isNil{
  85. return String.init(format: "%ld", middle)
  86. }
  87. return String.init(format: String.init(format:"%%.%df", decimalNumber!), Convert.toFloat(middle) ?? 0)
  88. }
  89. }
  90. else if object is Float{
  91. if let middle = object as? Float{
  92. if decimalNumber.isNil{
  93. return String.init(format: "%f", middle)
  94. }
  95. return String.init(format: String.init(format:"%%.%df", decimalNumber!), middle)
  96. }
  97. }
  98. else if object is Double{
  99. if let middle = object as? Double{
  100. if decimalNumber.isNil{
  101. return String.init(format: "%f", middle)
  102. }
  103. return String.init(format: String.init(format:"%%.%df", decimalNumber!), middle)
  104. }
  105. }
  106. else if object is String{
  107. if decimalNumber.isNotNil{
  108. if let middle = Convert.toDouble(object){
  109. return String.init(format: String.init(format:"%%.%df", decimalNumber!), middle)
  110. }
  111. }
  112. else{
  113. return object as? String
  114. }
  115. }
  116. return nil
  117. }
  118. static func toCGFloat(_ object: Any?) -> CGFloat{
  119. return CGFloat.init(Convert.toFloat(object) ?? 0)
  120. }
  121. }