#14: Known Unique References
Question
// Swift Quiz
— objc.io (@objcio) May 22, 2019
final class R { }
struct X {
var ref = R()
mutating func change() -> String {
return isKnownUniquelyReferenced(&ref) ? "No copy" : "Copy"
}
}
let x = X()
var array = [x]
array[0].change()
// What's the output?
Solution
Solution: Copy
— objc.io (@objcio) May 23, 2019
Because both the variable `x` as well as the array hold a reference to the same object, the `R` instance is not uniquely referenced, and the code will print "Copy".https://t.co/s7500CWmAg