Consider this ,
let value = true
print(value) —this will print true
But I can also do this,
print(true) it will print true.
This is not the case with optionals.
eg, let value: Int? = nil
print(value) —this will print nil
but the following does not work,
print(nil) —I get an error , ‘nil’ is not compatible with expected argument type ‘Any’
Why is this the case ?
Thx in advance
In the case off:
print(nil)
The compiler is expecting an argument of type Any (any variable or constant), in which nil isn’t Any. In other words, print is expecting to print the value of something (even if it’s nil), as opposed to printing the value of nothing.
I didn’t think of it that way . Thank you very much.
1 Like
This topic was automatically closed after 166 days. New replies are no longer allowed.