I have created one class which look like this
class IntegerReference {
var value = 10
}
Then i have allocate array firstIntegers which contains IntegerReference And anther array secondIntegersis just assign reference of firstIntegers
var firstIntegers = [IntegerReference(), IntegerReference()]
var secondIntegers = firstIntegers
Now i want to change the value of firstIntegers array
firstIntegers[0].value = 100
print(firstIntegers[0].value) //100 it is ok..
print(secondIntegers[0].value) //100 it is ok..
But when i want to modify firstIntegers array it will not affect on secondIntegers array
firstIntegers[0] = IntegerReference()
print(firstIntegers[0].value) //10 it is ok..
print(secondIntegers[0].value) // 100 Why 100? it should be 10 or Not?
secondIntegersstill has the instance of the first value (100). InfirstIntegers[0] = IntegerReference()you simply replaced the instance infirstIntegersbutsecondIntegersis still unaffected. Changing the value of an instance is because it's aclassrather than having anything to do with anArray.