This will not happen if I just change the properties instead of replacing the reference to a new object.
Here is a class Person, which is a reference type,
class Person {
var firstName: String
var lastName: String
init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
}
Here is an instance of Person,
var someone = Person(firstName: "Johnny", lastName: "Appleseed")
then I make an array containing values of type Person
var lotsOfPeople = [someone, someone, someone]
I suppose lotsOfPeople containing 3 references to someone. But if I change the third value in lotsOfPeople,
lotsOfPeople[2] = Person(firstName: "Lucy", lastName: "Swift")
someone itself isn't changed.
print(someone.firstName) // Johnny
I think it means lotsOfPeople[2] is not a reference to someone. How could this happen?