Predict the output
What happens?
final class User {
let id: Int
init(id: Int) { self.id = id }
}
let a = User(id: 1), b = User(id: 1)
print(a === b)
print(a == b)Hint 1 of 1
What does == require?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: C It doesn't compile, because User isn't Equatable
Correct. == needs Equatable. === alone would print false.
Why not the others?
- A false, false. The second line never runs; it doesn't compile.
- B false, true. Swift doesn't compare class properties automatically.
- D true, true. These are two different instances.