静态类型的 NSUserDefaults,使用了subscript下标
import UIKit
let keyPrefix = "me.fin.V2EXSettings."
class V2EXSettings: NSObject {
static let sharedInstance = V2EXSettings()
fileprivate override init(){
super.init()
}
subscript(key:String) -> String? {
get {
return UserDefaults.standard.object(forKey: keyPrefix + key) as? String
}
set {
UserDefaults.standard.setValue(newValue, forKey: keyPrefix + key )
}
}
}
调用的地方:
fileprivate static let STYLE_KEY = "styleKey"
fileprivate override init(){
if let style = V2EXSettings.sharedInstance[V2EXColor.STYLE_KEY] {
self.style = style
}
else{
self.style = V2EXColor.V2EXColorStyleDefault
}
super.init()
}
时间: 2024-10-09 09:24:35