From 3efef5a28ebdbbe922d4a2049493733ed14475a6 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 14 Sep 2023 20:42:48 +0700 Subject: [PATCH] Fix support for non-string keys in dictionaries (#149) --- Sources/Defaults/Defaults+Extensions.swift | 2 +- Tests/DefaultsTests/DefaultsDictionaryTests.swift | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Sources/Defaults/Defaults+Extensions.swift b/Sources/Defaults/Defaults+Extensions.swift index 17a2afc..ce2162e 100644 --- a/Sources/Defaults/Defaults+Extensions.swift +++ b/Sources/Defaults/Defaults+Extensions.swift @@ -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 { Defaults.DictionaryBridge() } } diff --git a/Tests/DefaultsTests/DefaultsDictionaryTests.swift b/Tests/DefaultsTests/DefaultsDictionaryTests.swift index 8ca3d15..a34b462 100644 --- a/Tests/DefaultsTests/DefaultsDictionaryTests.swift +++ b/Tests/DefaultsTests/DefaultsDictionaryTests.swift @@ -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"