File tree 2 files changed +8
-1
lines changed 2 files changed +8
-1
lines changed Original file line number Diff line number Diff line change @@ -86,7 +86,7 @@ public extension Dictionary where Key == String {
86
86
// Reached the end of the key path.
87
87
self [ head] = newValue as? Value
88
88
case let ( head, remainingKeyPath) ? :
89
- let value = self [ head]
89
+ let value : Any = self [ head] ?? [ : ]
90
90
if var nestedDictionary = value as? [ Key : Any ] {
91
91
// Key path has a tail we need to traverse
92
92
nestedDictionary [ keyPath: remainingKeyPath] = newValue
Original file line number Diff line number Diff line change @@ -92,4 +92,11 @@ final class KeyPathTests : XCTestCase {
92
92
let rhs = " this.is.path.end "
93
93
XCTAssertEqual ( KeyPath ( lhs) + KeyPath( rhs) , " this.is.path.start.this.is.path.end " )
94
94
}
95
+
96
+ func test_writingToEmptyDictionary( ) throws {
97
+ let data = " this is my data "
98
+ var dictionary : [ String : Any ] = [ : ]
99
+ dictionary [ keyPath: " nested.data " ] = data
100
+ XCTAssertEqual ( dictionary [ keyPath: " nested.data " ] as? String , data)
101
+ }
95
102
}
You can’t perform that action at this time.
0 commit comments