drmstfky
8/30/2018 - 9:34 AM

Value vs Reference

struct User {
    var fullName: String
    var email: String
    var age: Int
}

var someUser = User(fullName: "Mustafa KAYA", email: "mail@mustafakyaa.me", age: 28)

var anotherUser = someUser
anotherUser.email

someUser.email = "iletisim.mustafakaya@gmail.com"

someUser.email
anotherUser.email

class Person {
    var fullName: String
    var email: String
    var age: Int
    
    init(fullName: String, email: String, age: Int) {
        self.fullName = fullName
        self.email = email
        self.age = age
    }
}

var somePerson = Person(fullName: "Tim Cook", email: "tim.cook@apple.com", age: 54)

var anotherPerson = somePerson

somePerson.email = "tcook@apple.com"

anotherPerson.email
somePerson.email

// All structs are value types which means that the values are copied.
// Class is a reference type.