Last active
March 13, 2016 08:41
-
-
Save taji-taji/4247aa7f7a86ee042d2b to your computer and use it in GitHub Desktop.
【Swift】NSUserDefaultsを便利に使う方法を幾つか ref: http://qiita.com/taji-taji/items/edc1bad4709ce0e0a154
This file contains 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
enum UserDefaults { | |
// Bool型の値を持つキーをcaseで定義 | |
enum BoolValue: String { | |
case SomeFlag | |
func set(value: Bool) { | |
NSUserDefaults.standardUserDefaults().setBool(value, forKey: self.rawValue) | |
NSUserDefaults.standardUserDefaults().synchronize() | |
} | |
func get() -> Bool { | |
return NSUserDefaults.standardUserDefaults().boolForKey(self.rawValue) | |
} | |
} | |
// Int型の値を持つキーをcaseで定義 | |
enum IntValue: String { | |
case UserID | |
func set(value: Int) { | |
NSUserDefaults.standardUserDefaults().setInteger(value, forKey: self.rawValue) | |
NSUserDefaults.standardUserDefaults().synchronize() | |
} | |
func get() -> Int { | |
return NSUserDefaults.standardUserDefaults().integerForKey(self.rawValue) | |
} | |
} | |
// String型の値を持つキーをcaseで定義 | |
enum StringValue: String { | |
// ... | |
} | |
// 略... | |
} |
This file contains 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
UserDefaults.IntValue.UserID.set(1) | |
let userId = UserDefaults.IntValue.UserID.get() // 1 |
This file contains 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
struct UserDefaultsKeys { | |
// ここにキーを定義していく | |
static var hasLoginKey = DefaultsKey<Bool>("someFlag") | |
static var userID = DefaultsKey<Int>("userID") | |
} | |
struct DefaultsKey<ValueType> { | |
let key: String | |
init(_ key: String) { | |
self.key = key | |
} | |
} | |
extension NSUserDefaults { | |
subscript(key: DefaultsKey<Bool>) -> Bool { | |
get { | |
return boolForKey(key.key) | |
} | |
set { | |
setBool(newValue, forKey: key.key) | |
synchronize() | |
} | |
} | |
subscript(key: DefaultsKey<Int>) -> Int { | |
get { | |
return integerForKey(key.key) | |
} | |
set { | |
setInteger(newValue, forKey: key.key) | |
synchronize() | |
} | |
} | |
subscript(key: DefaultsKey<String>) -> String { | |
// ... | |
} | |
// 略... | |
} |
This file contains 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
let UserDefaults = NSUserDefaults.standardUserDefaults() | |
UserDefaults[UserDefaultsKeys.userID] = 1 | |
let userId = UserDefaults[UserDefaultsKeys.userID] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment