Skip to content

Instantly share code, notes, and snippets.

@syou007
Created May 16, 2017 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syou007/92110f02f14dbf510039362fe847d07d to your computer and use it in GitHub Desktop.
Save syou007/92110f02f14dbf510039362fe847d07d to your computer and use it in GitHub Desktop.
SwiftでSwiftyUserDefaultsに色々なデータを入れる。 ref: http://qiita.com/syou007/items/0fa4807862e6df9ff893
class Archiver: NSObject {
// 指定のデータをアーカイブします。
static func encrypt(rootObject: Any) -> Data {
return NSKeyedArchiver.archivedData(withRootObject: rootObject)
}
// アーカイブデータを解凍して返します。
static func decrypt<T>(data:Data) -> T? {
return NSKeyedUnarchiver.unarchiveObject(with: data) as? T
}
}
static var createdAt:Hoge? {
get {
if let data = Defaults[.createdAt:Date] {
return Archiver.decrypt(data: data)
}
return nil
}
set {
if let newValue = newValue {
Defaults[.createdAt:Date] = Archiver.encrypt(rootObject: newValue)
} else {
Defaults[.createdAt:Date] = nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment