Junior — Senior
What output will be obtained in example №6
livecode
Task condition
This example demonstrates how arrays containing reference objects behave when copied between different controllers. Two subclasses of the base controller are created, string arrays and view arrays are populated, and then one of the arrays is modified in the second controller. The arrays of the first controller are then printed to see what changes are reflected in its data.
class MyView: CustomStringConvertible {
let idx: UInt
var identifier: String
init(withIdx idx: UInt) {
self.idx = idx
identifier = String(idx)
}
var description: String {
return "(\(idx), \(identifier))"
}
}
class MyViewController {
var words = [String]()
var elements = [MyView]()
}
class FirstViewController: MyViewController {}
class SecondViewController: MyViewController {}
let firstVC = FirstViewController()
firstVC.words = ["One", "Two", "Three"]
firstVC.elements = [MyView(withIdx: 1), MyView(withIdx: 2), MyView(withIdx: 3)]
let secondVC = SecondViewController()
secondVC.words = firstVC.words
secondVC.elements = firstVC.elements
secondVC.words.popLast()
print(firstVC.words)
secondVC.elements.last?.identifier = "4"
print(firstVC.elements)