FasterThanLlamas
FasterThanLlamas

Reputation: 113

Swift 3 UserDefaults not saving

Totally stumped on why the below code isn't saving correctly. I have gone through and printed each variable individually to make sure they all have data and all the way to latestBottle everything is fine. But when I try to pull the value back from UserDefaults, it's 0.0.

func saveBottleEntry() {
    let allBottleEntries = realm.objects(SubmittedEntry.self).filter("bottleQuantity > 0")
    for bottle in allBottleEntries {
        bottleTimes.append(bottle.submissionTime!)
    }
    latestBottle = bottleTimes.max()!
    UserDefaults.standard.set(latestBottle, forKey: "latestBottle")
    print(UserDefaults.standard.double(forKey: "latestBottle"))
}

bottleTimes is an array of dates being retrieved from a Realm database. I then take the newest date bottleTimes.max() and save that to UserDefaults.

Upvotes: 1

Views: 725

Answers (1)

user3795616
user3795616

Reputation:

If you see 0.0 when try to get double that means that you wrote wrong type to UserDefaults

Upvotes: 1

Related Questions