Skip to content

Commit 3d321c1

Browse files
committed
Fix the new Swift NSMutableArray subclass to match edge case behavior in two more cases
1 parent ef64b13 commit 3d321c1

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

stdlib/public/core/SwiftNativeNSArray.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ extension __SwiftNativeNSArrayWithContiguousStorage: _NSArrayCore {
254254

255255
@objc(exchangeObjectAtIndex:withObjectAtIndex:)
256256
dynamic internal func exchange(at index: Int, with index2: Int) {
257-
swap(&contents[index], &contents[index2])
257+
contents.swapAt(index, index2)
258258
}
259259

260260
@objc(replaceObjectsInRange:withObjects:count:)
@@ -263,7 +263,13 @@ extension __SwiftNativeNSArrayWithContiguousStorage: _NSArrayCore {
263263
count: Int) {
264264
let range = range.location ..< range.location + range.length
265265
let buf = UnsafeBufferPointer(start: objects, count: count)
266-
contents.replaceSubrange(range, with: buf)
266+
if range == contents.startIndex..<contents.endIndex {
267+
contents = Array(buf)
268+
} else {
269+
// We make an Array here to make sure that something is holding onto the
270+
// objects in `buf`, since replaceSubrange could release them
271+
contents.replaceSubrange(range, with: Array(buf))
272+
}
267273
}
268274

269275
@objc(insertObjects:count:atIndex:)

0 commit comments

Comments
 (0)