Skip to content

Instantly share code, notes, and snippets.

@MoussaHellal
Last active April 26, 2023 15:46
Show Gist options
  • Save MoussaHellal/146428e00023966634bfa1e36a5f9183 to your computer and use it in GitHub Desktop.
Save MoussaHellal/146428e00023966634bfa1e36a5f9183 to your computer and use it in GitHub Desktop.
reference type in Swift
class Book {
var title: String
init(title: String) {
self.title = title
}
}
var bookOne = Book(title: "A Game of Thrones (1996)")
var bookTwo = bookOne // bookTwo now refers to the same instance as bookOne
bookTwo.title = "A Storm of Swords (2000)"
print(bookOne.title) // Output: "A Storm of Swords (2000)"
print(bookTwo.title) // Output: "A Storm of Swords (2000)"
print("bookOne memory address: \(Unmanaged.passUnretained(bookOne).toOpaque())") // Output: Memory Address
print("bookTwo memory address: \(Unmanaged.passUnretained(bookTwo).toOpaque())") // Output: Memory Address
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment