Junior — Middle
Kas koodis on võimalik võrrelda Optional-tüüpi objekte?
sobes.tech AI
Vastus AI-lt
In Swift, objects of type Optional can be compared with each other if the type they contain supports comparison. For example, if you have an Optional<Int>, you can use the == and != operators to compare them.
The comparison works as follows:
- If both values are
nil, they are considered equal. - If one is
niland the other is not, they are not equal. - If neither is
nil, their internal values are compared.
Example:
let a: Int? = 5
let b: Int? = 5
let c: Int? = nil
print(a == b) // true
print(a == c) // false
print(c == nil) // true
If the type does not support comparison, you cannot directly compare an Optional of that type.