How to get rid of `forKey` string literals in `UserDefaults` with Swift 3?

Alexander Vasenin

I'm sick of repeating countless coolFeatureEnabled string literals while working with UserDefaults. If there a good way to get rid of them with Swift 3?

var coolFeatureEnabled: Bool {
    get { return UserDefaults.standard.bool(forKey: "coolFeatureEnabled") }
    set { UserDefaults.standard.set(newValue, forKey: "coolFeatureEnabled") }
}
Alexander Vasenin

Here is how to avoid string literals with #function in Swift 3

// a little bit of setup

private func getBool(key: String = #function) -> Bool {
    return UserDefaults.standard.bool(forKey: key)
}

private func setBool(_ newValue: Bool, key: String = #function) {
    UserDefaults.standard.set(newValue, forKey: key)
}


// and here is the fun part

var coolFeatureEnabled: Bool {
    get { return getBool() }
    set { setBool(newValue) }
}

var anotherFeatureEnabled: Bool {
    get { return getBool() }
    set { setBool(newValue) }
}

...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get rid of a "non-breaking space" inside a Swift string?

From Dev

How to get rid of JSON unicode literals in Delphi/dwsJSON?

From Dev

How to get rid of a random "%3" added to the beginning of a string

From Dev

How to get rid of enter in String

From Dev

How to get rid of string concatenation for SQL query

From Dev

Accessing A Variable In UserDefaults Swift 3

From Dev

Escape backslashes in Swift string literals

From Dev

How to get rid of UINavigationController after doing a segue to a UITabBarController in Swift?

From Dev

NSCoder crash on decodeBool forKey (Xcode 8, Swift 3)

From Java

How do I get rid of the b-prefix in a string in python?

From Dev

How do I get rid of trailing and embedded spaces in a string?

From Dev

How do you get rid of null in a String in Java?

From Dev

How to get rid of all "weekday words" from a string

From Dev

How do I get rid of the b-prefix in a string in python?

From Dev

How to get rid of duplicate entries in a comma separated string

From Dev

How to get rid of all "weekday words" from a string

From Dev

How to get rid of Conversion from string "admin" to type 'Double' is not valid

From Dev

How to get rid of special characters but still keep spaces in formatting a string?

From Dev

how do I get rid of selected words from a string

From Dev

How do I get the length of a String in Swift3 on macOS

From Dev

How to get rid of margins

From Dev

How to get rid of "margin"

From Dev

How to get rid of cursors?

From Dev

How to get rid of duplicates?

From Dev

how to get rid of ChromiumOS

From Dev

How to get rid of this error

From Dev

How to get rid of the > sign?

From Dev

How to get rid of @{Alias=}?

From Dev

How to get rid of rEFInd?

Related Related

  1. 1

    How to get rid of a "non-breaking space" inside a Swift string?

  2. 2

    How to get rid of JSON unicode literals in Delphi/dwsJSON?

  3. 3

    How to get rid of a random "%3" added to the beginning of a string

  4. 4

    How to get rid of enter in String

  5. 5

    How to get rid of string concatenation for SQL query

  6. 6

    Accessing A Variable In UserDefaults Swift 3

  7. 7

    Escape backslashes in Swift string literals

  8. 8

    How to get rid of UINavigationController after doing a segue to a UITabBarController in Swift?

  9. 9

    NSCoder crash on decodeBool forKey (Xcode 8, Swift 3)

  10. 10

    How do I get rid of the b-prefix in a string in python?

  11. 11

    How do I get rid of trailing and embedded spaces in a string?

  12. 12

    How do you get rid of null in a String in Java?

  13. 13

    How to get rid of all "weekday words" from a string

  14. 14

    How do I get rid of the b-prefix in a string in python?

  15. 15

    How to get rid of duplicate entries in a comma separated string

  16. 16

    How to get rid of all "weekday words" from a string

  17. 17

    How to get rid of Conversion from string "admin" to type 'Double' is not valid

  18. 18

    How to get rid of special characters but still keep spaces in formatting a string?

  19. 19

    how do I get rid of selected words from a string

  20. 20

    How do I get the length of a String in Swift3 on macOS

  21. 21

    How to get rid of margins

  22. 22

    How to get rid of "margin"

  23. 23

    How to get rid of cursors?

  24. 24

    How to get rid of duplicates?

  25. 25

    how to get rid of ChromiumOS

  26. 26

    How to get rid of this error

  27. 27

    How to get rid of the > sign?

  28. 28

    How to get rid of @{Alias=}?

  29. 29

    How to get rid of rEFInd?

HotTag

Archive