I love extensions when they make the code cleaner:
extension NSUserDefaults {
func clear() {
guard let domainName = NSBundle.mainBundle().bundleIdentifier else {
return
}
self.removePersistentDomainForName(domainName)
}
}
Swift 5
extension UserDefaults {
func clear() {
guard let domainName = Bundle.main.bundleIdentifier else {
return
}
removePersistentDomain(forName: domainName)
synchronize()
}
}