Browse Source

【综合】批量页面范围优化和补充注释

tangchao 1 year ago
parent
commit
83c620908e
1 changed files with 12 additions and 7 deletions
  1. 12 7
      PDF Office/PDF Master/Class/Batch/Data/KMBatchBaseParameter.swift

+ 12 - 7
PDF Office/PDF Master/Class/Batch/Data/KMBatchBaseParameter.swift

@@ -51,16 +51,19 @@ import Cocoa
     
     func fetchUnlimitPagesArray() -> [NSNumber]? {
         var document = self._doc
-        if document == nil {
+        if document == nil { // 优化重复创建
             document = CPDFDocument(url: URL(fileURLWithPath: self.operateFile?.filePath ?? ""))
             self._doc = document
         }
-        if let data = document?.isLocked, data {
+        if let data = document?.isLocked, data { // 加锁的话,需要解锁
             if let data = self.operateFile?.password, data.isEmpty == false {
                 document?.unlock(withPassword: data)
             }
         }
         let pageNumber = document?.pageCount ?? 0
+        if pageNumber <= 0 { // 容错判断
+            return nil
+        }
         if self.pageChoice == .All {
             var selectPages: [NSNumber] = []
             for i in 1...pageNumber {
@@ -93,6 +96,10 @@ import Cocoa
                 }
             }
             if !isInvalid {
+                if let data = self.pageRangeString?.isEmpty, data { // 数据错误
+                    return nil
+                }
+                
                 let array = self.pageRangeString!.components(separatedBy: ",")
                 for s in array {
                     if s.isEmpty {
@@ -148,14 +155,12 @@ import Cocoa
                     }
                 }
             }
-            if (self.pageRangeString!.isEmpty) {
-                isInvalid = true
-            }
             if isInvalid {
                 return nil
             }
-            QuickSort(list: pageNumbers as! NSMutableArray, startIndex: 0, endIndex: pageNumbers.count - 1)
-            return pageNumbers
+            let tmpArray = NSMutableArray(array: pageNumbers)
+            QuickSort(list: tmpArray, startIndex: 0, endIndex: pageNumbers.count - 1)
+            return tmpArray as? [NSNumber]
         }
     }