Skip to content

Commit

Permalink
Fix support for non-string keys in dictionaries (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus authored Sep 14, 2023
1 parent 11b6adb commit 3efef5a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Defaults/Defaults+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ extension Array: Defaults.Serializable where Element: Defaults.Serializable {
}

extension Dictionary: Defaults.Serializable where Key: LosslessStringConvertible & Hashable, Value: Defaults.Serializable {
public static var isNativelySupportedType: Bool { Value.isNativelySupportedType }
public static var isNativelySupportedType: Bool { (Key.self is String.Type) && Value.isNativelySupportedType }
public static var bridge: Defaults.DictionaryBridge<Key, Value> { Defaults.DictionaryBridge() }
}

Expand Down
9 changes: 9 additions & 0 deletions Tests/DefaultsTests/DefaultsDictionaryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ final class DefaultsDictionaryTests: XCTestCase {
XCTAssertEqual(Defaults[key]["0"], [newName, fixtureArray[1]])
}

func testIntKey() {
let fixture = [1: "x"]
let key = Defaults.Key<[Int: String]>("independentDictionaryIntKey", default: fixture)
XCTAssertEqual(Defaults[key][1], fixture[1])
let newValue = "John"
Defaults[key][1] = newValue
XCTAssertEqual(Defaults[key][1], newValue)
}

func testType() {
XCTAssertEqual(Defaults[.dictionary]["0"], fixtureDictionary["0"])
let newName = "Chen"
Expand Down

0 comments on commit 3efef5a

Please sign in to comment.