123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- //
- // KMNTools.swift
- // PDF Reader Pro
- //
- // Created by 丁林圭 on 2024/10/24.
- //
- import Cocoa
- class KMNTools: NSObject {
-
- class func indexpathsToIndexs(indexpaths: Set<IndexPath>) -> IndexSet {
- var indexs = IndexSet()
- for indexPath in indexpaths {
- indexs.insert(indexPath.item)
- }
- return indexs
- }
-
- class func indexsToIndexpaths(indexs: IndexSet) -> Set<IndexPath> {
- var indexpaths = Set<IndexPath>()
- for index in indexs {
- indexpaths.insert(IndexPath(item: index, section: 0))
- }
- return indexpaths
- }
-
- class func newParseSelectedIndexs(selectedIndex: Array<Int>) -> String {
- if (selectedIndex.count == 0) {
- return ""
- }
-
- if (selectedIndex.count == 1) {
- return "\(selectedIndex.first!+1)"
- }
-
- var newDatas: Array<Int> = []
- for i in selectedIndex {
- newDatas.append(i)
- }
- /// 排序
- /// 根据id进行排序(升序)
- newDatas.sort(){$0 < $1}
-
- var a: Int = 0
- var b: Int = 0
- var result: String? = nil
- for i in newDatas {
- if (result == nil) {
- a = i
- b = i
- result = ""
- } else {
- if (i == b+1) {
- b = i
- if (i == newDatas.last) {
- result!.append(String(format: "%d-%d", a+1,b+1))
- }
- } else {
- if (a == b) {
- result!.append(String(format: "%d,", a+1))
- } else {
- result!.append(String(format: "%d-%d,", a+1,b+1))
- }
- a = i
- b = i
- if (i == newDatas.last) {
- result!.append(String(format: "%d", a+1))
- }
- }
- }
- }
-
- return result!
- }
-
- @objc class func parseIndexPathsSet(indexSets: Set<IndexPath>) -> String {
- var indexSet = IndexSet()
- for indeIndexPath in indexSets {
- indexSet.insert(indeIndexPath.item)
- }
- return self.parseIndexs(indexs: indexSet.sorted())
- }
- @objc class func parseIndexSet(indexSet: IndexSet) -> String {
- return self.parseIndexs(indexs: indexSet.sorted())
- }
-
- @objc class func parseIndexs(indexs: [Int]) -> String {
- if (indexs.isEmpty) {
- return ""
- }
- if (indexs.count == 1) {
- return "\(indexs.first!+1)"
- }
-
- var sortArray: [Int] = []
- for i in indexs {
- sortArray.append(i)
- }
- /// 排序 (升序)
- sortArray.sort(){$0 < $1}
-
- var a: Int = 0
- var b: Int = 0
- var result: String?
- for i in sortArray {
- if (result == nil) {
- a = i
- b = i
- result = ""
- continue
- }
- if (i == b+1) {
- b = i
- if (i == sortArray.last) {
- result?.append(String(format: "%d-%d", a+1,b+1))
- }
- } else {
- if (a == b) {
- result?.append(String(format: "%d,", a+1))
- } else {
- result?.append(String(format: "%d-%d,", a+1,b+1))
- }
- a = i
- b = i
- if (i == sortArray.last) {
- result?.append(String(format: "%d", a+1))
- }
- }
- }
- return result ?? ""
- }
- }
|