File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -86,7 +86,7 @@ public extension Dictionary where Key == String {
8686 // Reached the end of the key path.
8787 self [ head] = newValue as? Value
8888 case let ( head, remainingKeyPath) ? :
89- let value = self [ head]
89+ let value : Any = self [ head] ?? [ : ]
9090 if var nestedDictionary = value as? [ Key : Any ] {
9191 // Key path has a tail we need to traverse
9292 nestedDictionary [ keyPath: remainingKeyPath] = newValue
Original file line number Diff line number Diff line change @@ -92,4 +92,11 @@ final class KeyPathTests : XCTestCase {
9292 let rhs = " this.is.path.end "
9393 XCTAssertEqual ( KeyPath ( lhs) + KeyPath( rhs) , " this.is.path.start.this.is.path.end " )
9494 }
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+ }
95102}
You can’t perform that action at this time.
0 commit comments