Skip to content

Instantly share code, notes, and snippets.

@andr3a88
Created March 4, 2019 11:28
Show Gist options
  • Save andr3a88/165c180fee0152c2022485703b5a502b to your computer and use it in GitHub Desktop.
Save andr3a88/165c180fee0152c2022485703b5a502b to your computer and use it in GitHub Desktop.
An extension on UserDefaults, which’ll enable us to easily create an instance that has had all of its persisted values completely cleared
extension UserDefaults {
static func makeClearedInstance(
for functionName: StaticString = #function,
inFile fileName: StaticString = #file
) -> UserDefaults {
let className = "\(fileName)".split(separator: ".")[0]
let testName = "\(functionName)".split(separator: "(")[0]
let suiteName = "com.johnsundell.test.\(className).\(testName)"
let defaults = self.init(suiteName: suiteName)!
defaults.removePersistentDomain(forName: suiteName)
return defaults
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment