How to change the type of a value from UserDefaults?

user8537717

I'm trying to compare a value that was saved to UserDefaults to a new integer but I cant figure it out.

func setScore() {
    let defaults = UserDefaults.standard
    let newScore = score
    if defaults.object(forKey: "HighScore") != nil {
        defaults.set(newScore, forKey: "HighScore")
        let highScore = defaults.object(forKey: "HighScore") as! Int
        print(highScore)
    } else if defaults.object(forKey: "HighScore") < Int(newScore) {
        defaults.set(newScore, forKey: "HighScore")
        let highScore = defaults.object(forKey: "HighScore") as! Int
        print(highScore)
    } else {

    }
}

How can I change the value from defaults.object(forKey: "HighScore") to be an integer so I can compare them?

Dávid Pásztor

First of all, you can use UserDefaults.standar.integer(forKey:) to retrieve a value of type Int. Secondly, you should store the casted value once and shouldn't retrieve it several times (currently you retrieve it 3 times instead of just 1).

Moreover, your logic is flawed. You are trying to compare the value if the retrieved value was nil. So you are not just trying to compare Any? to Int, you are trying to compare nil to Int.

func setScore() {
    let defaults = UserDefaults.standard
    let newScore = score
    if let highScore = defaults.object(forKey: "HighScore") as? Int {
        print(highScore)
        if highScore < Int(newScore) {
            defaults.set(newScore, forKey: "HighScore")
        }
    }
}

The same function, but retrieving the Int value straight away without having to cast (UserDefaults.integer(forKey:) returns 0 if there is no value stored for that key).

func setScore() {
    let newScore = score
    if UserDefaults.standard.integer(forKey: "HighScore") < Int(newScore) {
            defaults.set(newScore, forKey: "HighScore")
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

how to change a Dataframe column from String type to Double type in pyspark

From Dev

How to unwrap an optional value from Any type?

From Dev

How to change array value from string to integer

From Dev

how to change input type from twig

From Dev

How to change name attribut value from twig

From Dev

How to Change Format column in Datagridview to date type for this value

From Dev

Change value on type=submit

From Dev

How to get the value type from an output iterator?

From Dev

How to Change Paypal amount value from javascript?

From Dev

How to change value from span tag?

From Dev

How to change the value of a child from a Mirror introspection

From Dev

How to change input type according to the option value?

From Dev

How to change the value of a primitive data type variable referred to by a foreach iterator

From Dev

how change type of ArrayList popped from stack from object to linkedlist?

From Dev

how can i change value type of a object in c++

From Dev

How to Change Format column in Datagridview to date type for this value

From Dev

How to change value of a variable from a file?

From Dev

How to find value of type from list

From Dev

How to pass value from change function?

From Dev

How to change the value of the "type" of an alias?

From Dev

How to access a value in type from a list

From Dev

how to use ldapmodifyuser from ldapscripts to change a value

From Dev

How to change the textbox value from checkbox

From Dev

how to change type of a value in elasticsearch

From Dev

Swfit - How can I use the UserDefaults to store variable and how to load the variable from UserDefaults next time

From Dev

How to correctly unset an array from UserDefaults in Swift

From Dev

How to get value from input type = number?

From Dev

How To change input type value in Laravel with if statement

From Dev

Why did I have to change the type of a the value of a map from & to (int*)&

Related Related

  1. 1

    how to change a Dataframe column from String type to Double type in pyspark

  2. 2

    How to unwrap an optional value from Any type?

  3. 3

    How to change array value from string to integer

  4. 4

    how to change input type from twig

  5. 5

    How to change name attribut value from twig

  6. 6

    How to Change Format column in Datagridview to date type for this value

  7. 7

    Change value on type=submit

  8. 8

    How to get the value type from an output iterator?

  9. 9

    How to Change Paypal amount value from javascript?

  10. 10

    How to change value from span tag?

  11. 11

    How to change the value of a child from a Mirror introspection

  12. 12

    How to change input type according to the option value?

  13. 13

    How to change the value of a primitive data type variable referred to by a foreach iterator

  14. 14

    how change type of ArrayList popped from stack from object to linkedlist?

  15. 15

    how can i change value type of a object in c++

  16. 16

    How to Change Format column in Datagridview to date type for this value

  17. 17

    How to change value of a variable from a file?

  18. 18

    How to find value of type from list

  19. 19

    How to pass value from change function?

  20. 20

    How to change the value of the "type" of an alias?

  21. 21

    How to access a value in type from a list

  22. 22

    how to use ldapmodifyuser from ldapscripts to change a value

  23. 23

    How to change the textbox value from checkbox

  24. 24

    how to change type of a value in elasticsearch

  25. 25

    Swfit - How can I use the UserDefaults to store variable and how to load the variable from UserDefaults next time

  26. 26

    How to correctly unset an array from UserDefaults in Swift

  27. 27

    How to get value from input type = number?

  28. 28

    How To change input type value in Laravel with if statement

  29. 29

    Why did I have to change the type of a the value of a map from & to (int*)&

HotTag

Archive