Browse Source

综合 - 修复历史记录为空时排序操作崩溃

wanjun 1 year ago
parent
commit
3b2562244b

+ 16 - 14
PDF Office/PDF Master/Class/Home/ViewController/KMHomeHistoryFileViewController.swift

@@ -444,21 +444,23 @@ class KMHomeHistoryFileViewController: NSViewController, NSCollectionViewDelegat
             }
         }
         
-        var dateArray: [Int: URL] = [:]
-        for index in 0...self.files.count - 1 {
-            let url = self.files[index]
-            let attrib = try? FileManager.default.attributesOfItem(atPath: url.path) as? Dictionary<FileAttributeKey , Any>
-            let fileDate: Date = attrib![FileAttributeKey(rawValue: "NSFileModificationDate")] as! Date
-            let timeInterval = fileDate.timeIntervalSince1970
-            dateArray.updateValue(url, forKey: Int(timeInterval))
-        }
-        
-        let sortedKeys = dateArray.keys.sorted(by: >)
+        if self.files.count > 0 {
+            var dateArray: [Int: URL] = [:]
+            for index in 0..<self.files.count {
+                let url = self.files[index]
+                let attrib = try? FileManager.default.attributesOfItem(atPath: url.path) as? Dictionary<FileAttributeKey , Any>
+                let fileDate: Date = attrib![FileAttributeKey(rawValue: "NSFileModificationDate")] as! Date
+                let timeInterval = fileDate.timeIntervalSince1970
+                dateArray.updateValue(url, forKey: Int(timeInterval))
+            }
 
-        files.removeAll()
-        for key in sortedKeys {
-            files.append(dateArray[key]!)
-            print("\(key): \(dateArray[key]!)")
+            let sortedKeys = dateArray.keys.sorted(by: >)
+
+            files.removeAll()
+            for key in sortedKeys {
+                files.append(dateArray[key]!)
+                print("\(key): \(dateArray[key]!)")
+            }
         }
         
         let fileNumber = KMPreferenceManager.shared.getData(forKey: KMPreference.documentMaximunDisplayNumberKey) as? Int ?? 10