Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
class Book {
deinit { print("bye") }
}
var anna: Book? = Book()
var boris = anna
anna = nil
print(1)
boris = nil
print(2)1 bye 2
- Expecting
bye 1 2: thinkinganna = nilfrees the object.borisstill holds a strong reference, so the object lives untilboris = nil. - Expecting
bye 1 bye 2: thinkingvar boris = annacopies the object. It copies the reference, so there is one object anddeinitruns once.
- What does the program print if the line
boris = nilis removed? - Why can this program not be written with
struct Bookinstead ofclass Book?