Skip to content

Instantly share code, notes, and snippets.

@Josscii
Created May 4, 2020 05:15
Show Gist options
  • Save Josscii/865d8a2943700a798a3c70e84fa8ab48 to your computer and use it in GitHub Desktop.
Save Josscii/865d8a2943700a798a3c70e84fa8ab48 to your computer and use it in GitHub Desktop.
比较好的 UserDefaults 的用法
//https://developer.apple.com/videos/play/wwdc2019/212
extension UserDefaults {
private static let isInfoBarHiddenKey = "IsInfoBarHidden"
@objc dynamic var isInfoBarHidden: Bool {
get { bool(forKey: UserDefaults.isInfoBarHiddenKey) }
set { set(newValue, forKey: UserDefaults.isInfoBarHiddenKey) }
}
}
func testUserDefaults() {
let _ = UserDefaults.standard.observe(\UserDefaults.isInfoBarHidden, options: .initial) { (_, value) in
if let newValue = value.newValue {
print(newValue)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment