With optional chaining, if I have a Swift variable
var s: String?
s might contain nil, or a String wrapped in an Optional. So, I tried this
to get its length:
let count = s?.characters?.count ?? 0
However, the compiler wants this:
let count = s?.characters.count ?? 0
let count = (s?.characters)?.count ?? 0
My understanding of optional chaining is that, once you start using '?.' in
a dotted expression, the rest of the properties evaluate as optional and
are typically accessed by '?.', not '.'.
So, I dug a little further and tried this in the playground:
var s: String? = "Foo"
The result indicates that s?.characters is indeed an Optional instance,
indicating that s?.characters.count should be illegal.
Why is s?.characters.count a legal expression?