Created
June 30, 2016 06:29
-
-
Save watura/f29ac6bc65ce2f669bce7841da47e1ec to your computer and use it in GitHub Desktop.
なんかUserDefaultsをちょっと楽に使う何か
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol LPType { | |
associatedtype ValueType | |
static var value: ValueType? {get set} | |
static func val() -> ValueType? | |
} | |
extension LPType { | |
static var value: ValueType? { | |
get { return Self.val() } | |
set { Self.set(newValue) } | |
} | |
private static var name: String { return "\(Self.self)"} | |
private static func set(object: ValueType?) { | |
if let object = object as? AnyObject? { | |
NSUserDefaults.standardUserDefaults().setObject(object, forKey: name) | |
} | |
} | |
} | |
extension LPType where ValueType == String { | |
static func val() -> ValueType? { | |
return NSUserDefaults.standardUserDefaults().stringForKey(name) | |
} | |
} | |
extension LPType where ValueType == Int { | |
static func val() -> ValueType? { | |
return NSUserDefaults.standardUserDefaults().integerForKey(name) | |
} | |
} | |
extension LPType where ValueType == Bool { | |
static func val() -> ValueType? { | |
return NSUserDefaults.standardUserDefaults().boolForKey(name) | |
} | |
} | |
extension LPType where ValueType == Double { | |
static func val() -> ValueType? { | |
return NSUserDefaults.standardUserDefaults().doubleForKey(name) | |
} | |
} | |
struct LocalPreference { | |
} | |
extension LocalPreference { | |
class Hoge: LPType { typealias ValueType = String } | |
class Fuga: LPType { typealias ValueType = Bool } | |
} | |
print(LocalPreference.Hoge.value) | |
LocalPreference.Hoge.value = "A" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment