changeset 193:47d61727054a

Fix crash when nil UserSettings
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 26 Feb 2021 15:17:57 +0000
parents 1ba446679981
children bba05cb7d62f
files LazyBear/ContentView.swift LazyBear/UI/Settings.swift UserSettings+CoreDataProperties.swift
diffstat 3 files changed, 5 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear/ContentView.swift	Fri Feb 26 15:17:41 2021 +0000
+++ b/LazyBear/ContentView.swift	Fri Feb 26 15:17:57 2021 +0000
@@ -40,7 +40,8 @@
                 .offset(y: hudManager.isShowing ? 0 : -100)
                 .animation(.easeInOut)
         }
-        .accentColor(Color("\(userSettings.first!.theme.lowercased())Accent"))
+        .accentColor(Color("\(userSettings.first?.theme?.lowercased() ?? "default")Accent"))
+        // If this value is not optional it will cause a crash
     }
 }
 
--- a/LazyBear/UI/Settings.swift	Fri Feb 26 15:17:41 2021 +0000
+++ b/LazyBear/UI/Settings.swift	Fri Feb 26 15:17:57 2021 +0000
@@ -31,7 +31,7 @@
     
     private func save(change: Any) {
         let userSettings = UserSettings(context: moc)
-        userSettings.theme = change as! String
+        userSettings.theme = change as? String
         userSettings.changedAt = Date()
         do {
             try moc.save()
--- a/UserSettings+CoreDataProperties.swift	Fri Feb 26 15:17:41 2021 +0000
+++ b/UserSettings+CoreDataProperties.swift	Fri Feb 26 15:17:57 2021 +0000
@@ -16,8 +16,8 @@
         return NSFetchRequest<UserSettings>(entityName: "UserSettings")
     }
 
-    @NSManaged public var appIcon: String
-    @NSManaged public var theme: String
+    @NSManaged public var appIcon: String?
+    @NSManaged public var theme: String?
     @NSManaged public var changedAt: Date
 
 }