From a4cd914b4d0f33f465bc28d41b4ab30e9a03f2f5 Mon Sep 17 00:00:00 2001 From: Abhi Date: Sun, 23 Apr 2023 15:34:42 -0400 Subject: [PATCH] Return nil if archiving fails. --- Clocker/Clocker/LocationController.swift | 4 +++- Clocker/Overall App/DataStore.swift | 4 +++- Clocker/Overall App/Foundation + Additions.swift | 4 +++- Clocker/Panel/Data Layer/TimezoneDataOperations.swift | 4 +++- Clocker/Panel/Notes Popover/NotesPopover.swift | 9 ++++++--- Clocker/Panel/ParentPanelController.swift | 4 +++- Clocker/Preferences/General/PreferencesDataSource.swift | 4 +++- .../Preferences/General/PreferencesViewController.swift | 4 +++- 8 files changed, 27 insertions(+), 10 deletions(-) diff --git a/Clocker/Clocker/LocationController.swift b/Clocker/Clocker/LocationController.swift index 46f3cb5..239118a 100644 --- a/Clocker/Clocker/LocationController.swift +++ b/Clocker/Clocker/LocationController.swift @@ -80,7 +80,9 @@ class LocationController: NSObject { var datas: [Data] = [] for updatedObject in timezoneObjects { - let dataObject = NSKeyedArchiver.clocker_archive(with: updatedObject) + guard let dataObject = NSKeyedArchiver.clocker_archive(with: updatedObject) else { + continue + } datas.append(dataObject) } diff --git a/Clocker/Overall App/DataStore.swift b/Clocker/Overall App/DataStore.swift index 495a017..6041b8c 100644 --- a/Clocker/Overall App/DataStore.swift +++ b/Clocker/Overall App/DataStore.swift @@ -128,7 +128,9 @@ class DataStore: NSObject { } func addTimezone(_ timezone: TimezoneData) { - let encodedTimezone = NSKeyedArchiver.clocker_archive(with: timezone) + guard let encodedTimezone = NSKeyedArchiver.clocker_archive(with: timezone) else { + return + } var defaults: [Data] = timezones() defaults.append(encodedTimezone) diff --git a/Clocker/Overall App/Foundation + Additions.swift b/Clocker/Overall App/Foundation + Additions.swift index ed911ef..dd271c6 100644 --- a/Clocker/Overall App/Foundation + Additions.swift +++ b/Clocker/Overall App/Foundation + Additions.swift @@ -58,7 +58,7 @@ public extension Data { } extension NSKeyedArchiver { - static func clocker_archive(with object: Any) -> Data { + static func clocker_archive(with object: Any) -> Data? { if #available(macOS 10.14, *) { return try! NSKeyedArchiver.archivedData(withRootObject: object, requiringSecureCoding: true) @@ -67,5 +67,7 @@ extension NSKeyedArchiver { if #available(macOS 10.13, *) { return NSKeyedArchiver.archivedData(withRootObject: object) } + + return nil } } diff --git a/Clocker/Panel/Data Layer/TimezoneDataOperations.swift b/Clocker/Panel/Data Layer/TimezoneDataOperations.swift index 9f3276f..5039542 100644 --- a/Clocker/Panel/Data Layer/TimezoneDataOperations.swift +++ b/Clocker/Panel/Data Layer/TimezoneDataOperations.swift @@ -421,7 +421,9 @@ extension TimezoneDataOperations { func saveObject(at index: Int = -1) { var defaults = store.timezones() - let encodedObject = NSKeyedArchiver.clocker_archive(with:dataObject as Any) + guard let encodedObject = NSKeyedArchiver.clocker_archive(with:dataObject as Any) else { + return + } index == -1 ? defaults.append(encodedObject) : defaults.insert(encodedObject, at: index) store.setTimezones(defaults) } diff --git a/Clocker/Panel/Notes Popover/NotesPopover.swift b/Clocker/Panel/Notes Popover/NotesPopover.swift index 01e0d3e..9862da8 100644 --- a/Clocker/Panel/Notes Popover/NotesPopover.swift +++ b/Clocker/Panel/Notes Popover/NotesPopover.swift @@ -315,8 +315,9 @@ class NotesPopover: NSViewController { } private func insertTimezoneInDefaultPreferences() { - guard let model = dataObject, var timezones = timezoneObjects else { return } - let encodedObject = NSKeyedArchiver.clocker_archive(with:model) + guard let model = dataObject, var timezones = timezoneObjects, + let encodedObject = NSKeyedArchiver.clocker_archive(with:model) else { return } + timezones[currentRow] = encodedObject DataStore.shared().setTimezones(timezones) } @@ -341,7 +342,9 @@ class NotesPopover: NSViewController { var datas: [Data] = [] for updatedObject in timezoneObjects { - let dataObject = NSKeyedArchiver.clocker_archive(with: updatedObject) + guard let dataObject = NSKeyedArchiver.clocker_archive(with: updatedObject) else { + continue + } datas.append(dataObject) } diff --git a/Clocker/Panel/ParentPanelController.swift b/Clocker/Panel/ParentPanelController.swift index 3a023ae..ca6d151 100644 --- a/Clocker/Panel/ParentPanelController.swift +++ b/Clocker/Panel/ParentPanelController.swift @@ -285,7 +285,9 @@ class ParentPanelController: NSWindowController { var datas: [Data] = [] for updatedObject in timezoneObjects { - let dataObject = NSKeyedArchiver.clocker_archive(with: updatedObject) + guard let dataObject = NSKeyedArchiver.clocker_archive(with: updatedObject) else { + continue + } datas.append(dataObject) } diff --git a/Clocker/Preferences/General/PreferencesDataSource.swift b/Clocker/Preferences/General/PreferencesDataSource.swift index e14d1f9..56544eb 100644 --- a/Clocker/Preferences/General/PreferencesDataSource.swift +++ b/Clocker/Preferences/General/PreferencesDataSource.swift @@ -184,7 +184,9 @@ extension PreferencesDataSource: NSTableViewDataSource { } private func insert(timezone: TimezoneData, at index: Int) { - let encodedObject = NSKeyedArchiver.clocker_archive(with: timezone) + guard let encodedObject = NSKeyedArchiver.clocker_archive(with: timezone) else { + return + } var newDefaults = selectedTimezones newDefaults[index] = encodedObject store.setTimezones(newDefaults) diff --git a/Clocker/Preferences/General/PreferencesViewController.swift b/Clocker/Preferences/General/PreferencesViewController.swift index 162ab20..61bc43c 100644 --- a/Clocker/Preferences/General/PreferencesViewController.swift +++ b/Clocker/Preferences/General/PreferencesViewController.swift @@ -977,7 +977,9 @@ extension PreferencesViewController: SRRecorderControlDelegate {} // Helpers extension PreferencesViewController { private func insert(timezone: TimezoneData, at index: Int) { - let encodedObject = NSKeyedArchiver.clocker_archive(with: timezone) + guard let encodedObject = NSKeyedArchiver.clocker_archive(with: timezone) else { + return + } var newDefaults = selectedTimeZones newDefaults[index] = encodedObject DataStore.shared().setTimezones(newDefaults)