Skip to content
Tom Ryan edited this page Jun 2, 2015 · 3 revisions

Classes are Reference Types

class TutorialClass {
    var firstVariable : Int

    init(firstVariable : Int) {
        self.firstVariable = firstVariable
    }
}

When copying a class, the copy is a reference to the original it was copied from.

var classTest = TutorialClass(firstVariable: 0)
var secondClassTest = classTest
println(classTest.firstVariable) // 0
println(secondClassTest.firstVariable) // 0

secondClassTest.firstVariable = 1
println(classTest.firstVariable) // 1
println(secondClassTest.firstVariable) // 1
  • Note that firstVariable in both classTest and secondClassTest are now both set to 1.