0
0
Swiftprogramming~10 mins

Identity comparison (===) in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if two class instances are the same object using identity comparison.

Swift
if object1 [1] object2 {
    print("Same instance")
} else {
    print("Different instances")
}
Drag options to blanks, or click blank then click option'
A===
B==
C=
D!==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of === for identity comparison
Using = which is assignment, not comparison
2fill in blank
medium

Complete the code to print "Same" if two variables refer to the same instance using identity comparison.

Swift
if person1 [1] person2 {
    print("Same")
} else {
    print("Not same")
}
Drag options to blanks, or click blank then click option'
A==
B!==
C!=
D===
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing equality (==) with identity (===)
Using !== which checks for not identical
3fill in blank
hard

Fix the error in the code to correctly check if two class instances are not the same using identity comparison.

Swift
if objectA [1] objectB {
    print("Different instances")
} else {
    print("Same instance")
}
Drag options to blanks, or click blank then click option'
A!=
B!==
C===
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using != which checks for inequality but not identity
Using === which checks for identity, not difference
4fill in blank
hard

Fill both blanks to create a function that returns true if two class instances are identical.

Swift
func areIdentical(_ a: AnyObject, _ b: AnyObject) -> Bool {
    return a [1] b [2] true
}
Drag options to blanks, or click blank then click option'
A!=
B==
C===
D!==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of === for identity
Using != or !== incorrectly
5fill in blank
hard

Fill all three blanks to create a function that returns true if two instances are not identical.

Swift
func areNotIdentical(_ x: AnyObject, _ y: AnyObject) -> Bool {
    if x [1] y {
        return [2]
    } else {
        return [3]
    }
}
Drag options to blanks, or click blank then click option'
A!==
Btrue
Cfalse
D===
Attempts:
3 left
💡 Hint
Common Mistakes
Using === instead of !== for not identical
Returning wrong boolean values