Skip to content

Commit e64d8d0

Browse files
authored
Merge pull request #26 from AdamsDevelopment/feature/add_enum_defaults
Add enum to defaults
2 parents dde954d + 26b87ba commit e64d8d0

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

Example/Tests/Specs/Defaults/DefaultsSpec.swift

+58
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,24 @@ import Quick
1010
import Nimble
1111
import Stella
1212

13+
/// An enum for our enumStringTypeValue test case
14+
enum SomeStringEnum: String {
15+
case type1
16+
case type2
17+
case type3
18+
}
19+
20+
/// An enum for our enumStringTypeValue test case
21+
enum SomeIntEnum: Int {
22+
case type1
23+
case type2
24+
case type3
25+
}
26+
1327
// Define the keys used for this test.
1428
extension DefaultsKeys {
29+
static let enumStringTypeValue = DefaultsKey<SomeStringEnum?>("enumStringTypeValue")
30+
static let enumIntTypeValue = DefaultsKey<SomeIntEnum?>("enumIntTypeValue")
1531
static let stringValue = DefaultsKey<String?>("stringValue")
1632
static let integerValue = DefaultsKey<Int?>("integerValue")
1733
static let doubleValue = DefaultsKey<Double?>("doubleValue")
@@ -25,6 +41,48 @@ class DefaultsSpec: QuickSpec {
2541
override func spec() {
2642

2743
describe("defaults") {
44+
context("enumType value + String") {
45+
it("should be able to write to the defaults") {
46+
Defaults[.enumStringTypeValue] = .type1
47+
let enumTypeValue = SomeStringEnum(rawValue: UserDefaults.standard.string(forKey: "enumStringTypeValue") ?? "")
48+
expect(enumTypeValue).to(equal(.type1))
49+
}
50+
51+
it("should be able to clear the defaults") {
52+
Defaults[.enumStringTypeValue] = .type1
53+
Defaults[.enumStringTypeValue] = nil
54+
let stringValue = SomeStringEnum(rawValue: UserDefaults.standard.string(forKey: "enumStringTypeValue") ?? "")
55+
expect(stringValue).to(beNil())
56+
}
57+
58+
it("should be able to read from the defaults") {
59+
UserDefaults.standard.set("type1", forKey: "enumStringTypeValue")
60+
expect(Defaults[.enumStringTypeValue]).to(equal(.type1))
61+
}
62+
}
63+
64+
context("enumType value + Int") {
65+
it("should be able to write to the defaults") {
66+
Defaults[.enumIntTypeValue] = .type1
67+
let enumTypeValue = SomeIntEnum(rawValue: UserDefaults.standard.integer(forKey: "enumIntTypeValue"))
68+
expect(enumTypeValue).to(equal(.type1))
69+
}
70+
71+
// We will not be able to clear the defaults because this will always return a 0 integer
72+
// 0 integer == first case -> .type1
73+
it("should not be able to clear the defaults") {
74+
Defaults[.enumIntTypeValue] = .type1
75+
Defaults[.enumIntTypeValue] = nil
76+
let stringValue = SomeIntEnum(rawValue: UserDefaults.standard.integer(forKey: "enumIntTypeValue"))
77+
expect(stringValue).to(equal(.type1))
78+
}
79+
80+
it("should be able to read from the defaults") {
81+
UserDefaults.standard.set("type1", forKey: "enumIntTypeValue")
82+
expect(Defaults[.enumIntTypeValue]).to(equal(.type1))
83+
}
84+
}
85+
2886
context("string value") {
2987
it("should be able to write to the defaults") {
3088
Defaults[.stringValue] = "A string value"

Sources/Defaults/Defaults.swift

+29
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,35 @@ open class DefaultsKey<ValueType>: DefaultsKeys {
3434

3535
public extension UserDefaults {
3636

37+
/// Get the defaults Enum (type String) value for the given `DefaultsKey`. The preferred way to do this is to pass the static key variable defined in the `DefaultsKeys` extension.
38+
///
39+
/// ```
40+
/// static let enumType = DefaultsKey<SomeEnumStringType?>("the enumType's defaults key")
41+
/// ```
42+
public subscript<T: RawRepresentable>(key: DefaultsKey<T?>) -> T? where T.RawValue == String {
43+
get {
44+
guard let rawValue = string(forKey: key.key) else { return nil }
45+
return T(rawValue: rawValue)
46+
}
47+
set {
48+
set(newValue?.rawValue, forKey: key.key)
49+
}
50+
}
51+
52+
/// Get the defaults Enum (type Int) value for the given `DefaultsKey`. The preferred way to do this is to pass the static key variable defined in the `DefaultsKeys` extension.
53+
///
54+
/// ```
55+
/// static let enumType = DefaultsKey<SomeEnumIntType?>("the enumType's defaults key")
56+
/// ```
57+
public subscript<T: RawRepresentable>(key: DefaultsKey<T?>) -> T? where T.RawValue == Int {
58+
get {
59+
return T(rawValue: integer(forKey: key.key))
60+
}
61+
set {
62+
set(newValue?.rawValue, forKey: key.key)
63+
}
64+
}
65+
3766
/// Get the defaults String value for the given `DefaultsKey`. The preferred way to do this is to pass the static key variable defined in the `DefaultsKeys` extension.
3867
///
3968
/// ```

0 commit comments

Comments
 (0)