Sobes.tech
Junior — Middle

What data types in Swift are reference types?

sobes.tech AI

Answer from AI

In Swift, reference types are classes and closures. They are stored in the heap, and when assigned or passed, a reference to the same object is copied.

Primitive types, structures (struct), and enumerations (enum) are value types, which are copied upon assignment.

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 print "Peter" because person1 and person2 refer to the same object
What data types in Swift are reference types? — iOS - sobes.tech