Skip to content

Commit 1750e04

Browse files
committed
Look up and reuse the container when asking for the nested container with the same key in PlistEncoder
1 parent 32ef877 commit 1750e04

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

stdlib/public/Darwin/Foundation/PlistEncoder.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,9 @@ fileprivate struct _PlistKeyedEncodingContainer<K : CodingKey> : KeyedEncodingCo
265265
}
266266

267267
public mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> {
268-
let dictionary = NSMutableDictionary()
269-
self.container[key.stringValue] = dictionary
268+
let containerKey = key.stringValue
269+
let dictionary = self.container[containerKey] as? NSMutableDictionary ?? NSMutableDictionary()
270+
self.container[containerKey] = dictionary
270271

271272
self.codingPath.append(key)
272273
defer { self.codingPath.removeLast() }
@@ -276,8 +277,9 @@ fileprivate struct _PlistKeyedEncodingContainer<K : CodingKey> : KeyedEncodingCo
276277
}
277278

278279
public mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
279-
let array = NSMutableArray()
280-
self.container[key.stringValue] = array
280+
let containerKey = key.stringValue
281+
let array = self.container[containerKey] as? NSMutableArray ?? NSMutableArray()
282+
self.container[containerKey] = array
281283

282284
self.codingPath.append(key)
283285
defer { self.codingPath.removeLast() }

0 commit comments

Comments
 (0)