Skip to content

Commit 2e39c29

Browse files
committed
improves management of file properties that could return error (modifiedDate and filesize) if no longer exist.
1 parent 17a7eee commit 2e39c29

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

Diff for: OpenGpxTracker/GPXFileInfo.swift

+10-6
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,25 @@ class GPXFileInfo: NSObject {
1717
/// file URL
1818
var fileURL: URL = URL(fileURLWithPath: "")
1919

20-
/// Last time the file was modified
2120
var modifiedDate: Date {
22-
// swiftlint:disable:next force_try
23-
return try! fileURL.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate ?? Date.distantPast
21+
guard let resourceValues = try? fileURL.resourceValues(forKeys: [.contentModificationDateKey]),
22+
let modificationDate = resourceValues.contentModificationDate else {
23+
return Date.distantPast // Default value if the modification date cannot be retrieved
24+
}
25+
return modificationDate
2426
}
25-
2627
/// modified date has a time ago string (for instance: 3 days ago)
2728
var modifiedDatetimeAgo: String {
2829
return modifiedDate.timeAgo(numericDates: true)
2930
}
3031

3132
/// File size in bytes
3233
var fileSize: Int {
33-
// swiftlint:disable:next force_try
34-
return try! fileURL.resourceValues(forKeys: [.fileSizeKey]).fileSize ?? 0
34+
guard let resourceValues = try? fileURL.resourceValues(forKeys: [.fileSizeKey]),
35+
let size = resourceValues.fileSize else {
36+
return -1 // Default value if the file size cannot be retrieved
37+
}
38+
return size
3539
}
3640

3741
/// File size as string in a more readable format (example: 10 KB)

0 commit comments

Comments
 (0)