Complete the code to check if two class instances are the same object using identity comparison.
if object1 [1] object2 { print("Same instance") } else { print("Different instances") }
In Swift, the identity comparison operator is ===. It checks if two references point to the exact same object.
Complete the code to print "Same" if two variables refer to the same instance using identity comparison.
if person1 [1] person2 { print("Same") } else { print("Not same") }
The === operator checks if two variables refer to the same instance in memory.
Fix the error in the code to correctly check if two class instances are not the same using identity comparison.
if objectA [1] objectB { print("Different instances") } else { print("Same instance") }
The operator !== checks if two references do not point to the same object.
Fill both blanks to create a function that returns true if two class instances are identical.
func areIdentical(_ a: AnyObject, _ b: AnyObject) -> Bool {
return a [1] b [2] true
}The function uses === to check identity and returns true if they are the same instance.
Fill all three blanks to create a function that returns true if two instances are not identical.
func areNotIdentical(_ x: AnyObject, _ y: AnyObject) -> Bool {
if x [1] y {
return [2]
} else {
return [3]
}
}The function uses !== to check if two references are not identical, returning true if so, otherwise false.