Junior — Middle
Can you explain what a reference type is?
sobes.tech AI
Answer from AI
A reference type is a type where variables store not the data itself, but a reference (pointer) to the memory area where the data is located. In Swift, for example, classes are reference types. This means that when assigning or passing a class object, a reference to the same instance is copied, not the object itself.
Example:
class Person {
var name: String
init(name: String) {
self.name = name
}
}
let person1 = Person(name: "Ivan")
let person2 = person1
person2.name = "Peter"
print(person1.name) // Will output "Peter" because person1 and person2 refer to the same object
This differs from value types (e.g., structs), where the object itself is copied.