0
0
Swiftprogramming~10 mins

Identity operators (=== and !==) 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 refer to the same object.

Swift
if object1 [1] object2 {
    print("Same instance")
}
Drag options to blanks, or click blank then click option'
A==
B!==
C!=
D===
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of === to check identity.
Confusing identity operators with equality operators.
2fill in blank
medium

Complete the code to check if two class instances do NOT refer to the same object.

Swift
if objectA [1] objectB {
    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 equality operators instead of identity operators.
3fill in blank
hard

Fix the error in the code to correctly check identity between two instances.

Swift
if person1 [1] person2 {
    print("They are the same person")
}
Drag options to blanks, or click blank then click option'
A===
B==
C!=
D!==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == which checks value equality, not identity.
Using !== which checks for non-identity.
4fill in blank
hard

Fill both blanks to create a condition that prints "Same" if two objects are identical and "Different" if not.

Swift
if obj1 [1] obj2 {
    print("Same")
} else if obj1 [2] obj2 {
    print("Different")
}
Drag options to blanks, or click blank then click option'
A===
B==
C!==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using equality operators (==, !=) instead of identity operators.
Mixing up the order of operators.
5fill in blank
hard

Fill both blanks to create a function that returns true if two references are identical and false otherwise.

Swift
func areIdentical(_ a: AnyObject, _ b: AnyObject) -> Bool {
    return a [1] b [2] true : false
}
Drag options to blanks, or click blank then click option'
A===
B==
C?
D:
Attempts:
3 left
💡 Hint
Common Mistakes
Using equality operator instead of identity operator.
Forgetting to use the ternary operator correctly.