Skip to content

Instantly share code, notes, and snippets.

@JanGorman
Last active March 7, 2019 09:49
Show Gist options
  • Select an option

  • Save JanGorman/aeb31347bda571771b60bec55ba3ad9b to your computer and use it in GitHub Desktop.

Select an option

Save JanGorman/aeb31347bda571771b60bec55ba3ad9b to your computer and use it in GitHub Desktop.
// Combine app + shared defaults into one
extension UserDefaults {
static var shared: UserDefaults {
let combined = UserDefaults.standard
combined.addSuite(named: "com.schnaub.app")
return combined
}
}
// Unit test instance that clears all values before the test is run
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.schnaub.text.\(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