KMNTools.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // KMNTools.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/10/24.
  6. //
  7. import Cocoa
  8. class KMNTools: NSObject {
  9. class func indexpathsToIndexs(indexpaths: Set<IndexPath>) -> IndexSet {
  10. var indexs = IndexSet()
  11. for indexPath in indexpaths {
  12. indexs.insert(indexPath.item)
  13. }
  14. return indexs
  15. }
  16. class func indexsToIndexpaths(indexs: IndexSet) -> Set<IndexPath> {
  17. var indexpaths = Set<IndexPath>()
  18. for index in indexs {
  19. indexpaths.insert(IndexPath(item: index, section: 0))
  20. }
  21. return indexpaths
  22. }
  23. class func newParseSelectedIndexs(selectedIndex: Array<Int>) -> String {
  24. if (selectedIndex.count == 0) {
  25. return ""
  26. }
  27. if (selectedIndex.count == 1) {
  28. return "\(selectedIndex.first!+1)"
  29. }
  30. var newDatas: Array<Int> = []
  31. for i in selectedIndex {
  32. newDatas.append(i)
  33. }
  34. /// 排序
  35. /// 根据id进行排序(升序)
  36. newDatas.sort(){$0 < $1}
  37. var a: Int = 0
  38. var b: Int = 0
  39. var result: String? = nil
  40. for i in newDatas {
  41. if (result == nil) {
  42. a = i
  43. b = i
  44. result = ""
  45. } else {
  46. if (i == b+1) {
  47. b = i
  48. if (i == newDatas.last) {
  49. result!.append(String(format: "%d-%d", a+1,b+1))
  50. }
  51. } else {
  52. if (a == b) {
  53. result!.append(String(format: "%d,", a+1))
  54. } else {
  55. result!.append(String(format: "%d-%d,", a+1,b+1))
  56. }
  57. a = i
  58. b = i
  59. if (i == newDatas.last) {
  60. result!.append(String(format: "%d", a+1))
  61. }
  62. }
  63. }
  64. }
  65. return result!
  66. }
  67. @objc class func parseIndexPathsSet(indexSets: Set<IndexPath>) -> String {
  68. var indexSet = IndexSet()
  69. for indeIndexPath in indexSets {
  70. indexSet.insert(indeIndexPath.item)
  71. }
  72. return self.parseIndexs(indexs: indexSet.sorted())
  73. }
  74. @objc class func parseIndexSet(indexSet: IndexSet) -> String {
  75. return self.parseIndexs(indexs: indexSet.sorted())
  76. }
  77. @objc class func parseIndexs(indexs: [Int]) -> String {
  78. if (indexs.isEmpty) {
  79. return ""
  80. }
  81. if (indexs.count == 1) {
  82. return "\(indexs.first!+1)"
  83. }
  84. var sortArray: [Int] = []
  85. for i in indexs {
  86. sortArray.append(i)
  87. }
  88. /// 排序 (升序)
  89. sortArray.sort(){$0 < $1}
  90. var a: Int = 0
  91. var b: Int = 0
  92. var result: String?
  93. for i in sortArray {
  94. if (result == nil) {
  95. a = i
  96. b = i
  97. result = ""
  98. continue
  99. }
  100. if (i == b+1) {
  101. b = i
  102. if (i == sortArray.last) {
  103. result?.append(String(format: "%d-%d", a+1,b+1))
  104. }
  105. } else {
  106. if (a == b) {
  107. result?.append(String(format: "%d,", a+1))
  108. } else {
  109. result?.append(String(format: "%d-%d,", a+1,b+1))
  110. }
  111. a = i
  112. b = i
  113. if (i == sortArray.last) {
  114. result?.append(String(format: "%d", a+1))
  115. }
  116. }
  117. }
  118. return result ?? ""
  119. }
  120. }