'I'm trying to test COW on Collection Types. But it didn't work

I'm trying to test COW on Collection Types such as Arrays, Set and dictionaries. But it didn't work on Set and Dictionaries. Here're my code and memory addresses of them. Please help.


var aArray: [Int] = [0, 1, 2, 3]
var bArray = aArray

var aSet: Set = [0, 1, 2]
var bSet = aSet

var aDictionary = ["0": 0]

for i in 0..<10000 {
    aDictionary.updateValue(i, forKey: "\(i)")
}
var bDictionary = aDictionary

address(of: &aArray) // 0x600001370ea0
address(of: &bArray) // 0x600001370ea0 // Array is okay
address(of: &aSet) // 0x10244c5a0
address(of: &bSet) // 0x10244c5a8 
address(of: &aDictionary) // 0x10244c5b0
address(of: &bDictionary) // 0x10244c5b8 // But they are different

I expected Set and Dictionaries had same addresses before modifying values because of COW.



Solution 1:[1]

These are value type variables not reference type, so it may not refer to same memory address. You can use reference DataType like NSArray, NSSet, etc

var aArray1: NSArray = [0, 1, 2, 3]
var bArray1 = aArray1

var aSet1: NSSet = [0, 1, 2]
var bSet1 = aSet1

Now if you will check the reference will be same

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Janmenjaya