Skip to content

Commit

Permalink
Simplify if let unwrapping for userdefaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry-KNIGHT committed Nov 29, 2023
1 parent d035ecc commit ddd79db
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
9 changes: 4 additions & 5 deletions ControlRoom/Controllers/ColorHistoryController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ class ColorHistoryController: ObservableObject {

/// Attempts to load saved colors from UserDefaults, or creates an empty array otherwise.
init() {
if let data = UserDefaults.standard.data(forKey: defaultsKey) {
if let decoded = try? JSONDecoder().decode([PickedColor].self, from: data) {
colors = decoded
return
}
if let data = UserDefaults.standard.data(forKey: defaultsKey),
let decoded = try? JSONDecoder().decode([PickedColor].self, from: data) {
colors = decoded
return
}

colors = []
Expand Down
9 changes: 4 additions & 5 deletions ControlRoom/Controllers/DeepLinksController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ class DeepLinksController: ObservableObject {

/// Attempts to load saved links from UserDefaults, or creates an empty array otherwise.
init() {
if let data = UserDefaults.standard.data(forKey: defaultsKey) {
if let decoded = try? JSONDecoder().decode([DeepLink].self, from: data) {
links = decoded
return
}
if let data = UserDefaults.standard.data(forKey: defaultsKey),
let decoded = try? JSONDecoder().decode([DeepLink].self, from: data) {
links = decoded
return
}

links = []
Expand Down
9 changes: 4 additions & 5 deletions ControlRoom/Controllers/LocationsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ class LocationsController: ObservableObject {

/// Attempts to load saved locations from UserDefaults, or creates an empty array otherwise.
init() {
if let data = UserDefaults.standard.data(forKey: defaultsKey) {
if let decoded = try? JSONDecoder().decode([Location].self, from: data) {
locations = decoded
return
}
if let data = UserDefaults.standard.data(forKey: defaultsKey),
let decoded = try? JSONDecoder().decode([Location].self, from: data) {
locations = decoded
return
}

locations = []
Expand Down

0 comments on commit ddd79db

Please sign in to comment.