Last active
April 26, 2023 15:46
-
-
Save MoussaHellal/146428e00023966634bfa1e36a5f9183 to your computer and use it in GitHub Desktop.
reference type in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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