Created
May 16, 2017 14:10
-
-
Save syou007/92110f02f14dbf510039362fe847d07d to your computer and use it in GitHub Desktop.
SwiftでSwiftyUserDefaultsに色々なデータを入れる。 ref: http://qiita.com/syou007/items/0fa4807862e6df9ff893
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
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 | |
} | |
} |
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
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