Use native data coding (#5)

This commit is contained in:
Lars-Jørgen Kristiansen 2018-04-15 20:09:53 +02:00 committed by Sindre Sorhus
parent c78c1b00b6
commit 3b7f63bc90
2 changed files with 11 additions and 1 deletions

View File

@ -115,7 +115,8 @@ public extension UserDefaults {
is Int.Type, is Int.Type,
is Double.Type, is Double.Type,
is Float.Type, is Float.Type,
is Date.Type: is Date.Type,
is Data.Type:
return true return true
default: default:
return false return false

View File

@ -13,6 +13,7 @@ extension Defaults.Keys {
static let key = Defaults.Key<Bool>("key", default: false) static let key = Defaults.Key<Bool>("key", default: false)
static let url = Defaults.Key<URL>("url", default: fixtureUrl) static let url = Defaults.Key<URL>("url", default: fixtureUrl)
static let `enum` = Defaults.Key<FixtureEnum>("enum", default: .oneHour) static let `enum` = Defaults.Key<FixtureEnum>("enum", default: .oneHour)
static let data = Defaults.Key<Data>("data", default: Data(bytes: []))
} }
final class DefaultsTests: XCTestCase { final class DefaultsTests: XCTestCase {
@ -53,6 +54,14 @@ final class DefaultsTests: XCTestCase {
XCTAssertEqual(defaults[.enum], FixtureEnum.oneHour) XCTAssertEqual(defaults[.enum], FixtureEnum.oneHour)
} }
func testDataType() {
XCTAssertEqual(defaults[.data], Data(bytes: []))
let newData = Data(bytes: [0xFF])
defaults[.data] = newData
XCTAssertEqual(defaults[.data], newData)
}
func testClear() { func testClear() {
defaults[.key] = true defaults[.key] = true
XCTAssertTrue(defaults[.key]) XCTAssertTrue(defaults[.key])