Last active
May 11, 2023 06:08
-
-
Save djryanash/a2e64e59f3961d5da18272aa9e028c6e to your computer and use it in GitHub Desktop.
Check userDefaults in Swift/Xcode for 'User()'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User: Codable { | |
static let shared = User() | |
var hasBeenUpdated = false | |
var completedTasks: Int = 0 | |
var currentlyViewingTask: Int = 0 | |
var milestoneNumber: Int = 0 | |
var totalMilestones: Int = 0 | |
var milestones: [Int] = [1,2,5,10,20,40] | |
var canEdit: Bool = false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
func checkUserDefaults() { | |
var user = User.shared | |
let userData = "userData" | |
if let dataUser = userDefaults.data(forKey: userData) { | |
let decoder = JSONDecoder() | |
do { | |
self.user = try decoder.decode(User.self, from: dataUser) | |
} catch { | |
fatalError("fatalError: \(error)") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment