Last active
August 27, 2015 05:55
-
-
Save el-hoshino/1ca9f37bf240d0e452bd to your computer and use it in GitHub Desktop.
NSUserDefaults で struct を保存(?)する方法 ref: http://qiita.com/lovee/items/2b39e1c54d6d00adead9
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
class ViewController: UIViewController { | |
var settings: UserSettings | |
init() { | |
self.settings = UserSettings(a: 0, b: false, c: "NO") | |
super.init(nibName: nil, bundle: nil) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
struct UserSettings { | |
var a: Int | |
var b: Bool | |
var c: String | |
} |
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
struct UserSettings { | |
var a: Int | |
var b: Bool | |
var c: String | |
} | |
class UserSettingsObject { | |
var a: Int | |
var b: Bool | |
var c: String | |
// すでに設定があれば流用します | |
init(settings: UserSettings) { | |
self.a = settings.a | |
self.b = settings.b | |
self.c = settings.c | |
} | |
// まだ設定が作られていない場合はデフォルト設定を作ります | |
init() { | |
self.a = 0 | |
self.b = false | |
self.c = "NO" | |
} | |
} | |
class ViewController: UIViewController { | |
let userDefaults = NSUserDefaults.standardUserDefaults() | |
var settings: UserSettings | |
init() { | |
let settingsObject = self.userDefaults.valueForKey("SaveData") as? UserSettingsObject ?? UserSettingsObject() | |
self.settings = UserSettings(a: settingsObject.a, b: settingsObject.b, c: settingsObject.c) | |
super.init(nibName: nil, bundle: nil) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
func save() { | |
let settingObject = UserSettingsObject(settings: self.settings) | |
self.userDefaults.setValue(settingObject, forKey: "SaveData") | |
} | |
} |
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
struct UserSettings { | |
var a: Int | |
var b: Bool | |
var c: String | |
static func userSettingsFromNSDictionary(dictionary: NSDictionary?) -> UserSettings { | |
let aValue = dictionary?["a"] as? Int ?? 0 | |
let bValue = dictionary?["b"] as? Bool ?? false | |
let cValue = dictionary?["c"] as? String ?? "NO" | |
return UserSettings(a: aValue, b: bValue, c: cValue) | |
} | |
func nsdictionaryForNSUserDefaults() -> NSDictionary { | |
let dictionary: NSDictionary = [ | |
"a": self.a, | |
"b": self.b, | |
"c": self.c, | |
] | |
return dictionary | |
} | |
} | |
class ViewController: UIViewController { | |
let userDefaults = NSUserDefaults.standardUserDefaults() | |
var settings: UserSettings | |
init() { | |
let settingsDictionary = self.userDefaults.valueForKey("SaveData") as? NSDictionary | |
self.settings = UserSettings.userSettingsFromNSDictionary(settingsDictionary) | |
super.init(nibName: nil, bundle: nil) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
func save() { | |
let settingDictionary = self.settings.nsdictionaryForNSUserDefaults() | |
self.userDefaults.setValue(settingDictionary, forKey: "SaveData") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment