blog
blog copied to clipboard
How to use subscript in Swift
Make it easy to access common cases, for example UserDefaults
extension UserDefaults {
enum Key: String {
case hasBackup
}
subscript(key: Key) -> Bool {
get {
bool(forKey: key.rawValue)
}
set {
set(newValue, forKey: key.rawValue)
}
}
}
UserDefaults.standard.hasBackup] = true
the last line should be UserDefaults.standard[.hasBackup] = true