<div dir="ltr">With optional chaining, if I have a Swift variable<br><br> var s: String?<br><br>s might contain nil, or a String wrapped in an Optional. So, I tried this to get its length:<br><br> let count = s?.characters?.count ?? 0<br><br>However, the compiler wants this:<br><br> let count = s?.characters.count ?? 0<div><br></div><div>or this:</div><div><br></div><div> let count = (s?.characters)?.count ?? 0<br><br>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 '.'.<br><br>So, I dug a little further and tried this in the playground:<br><br>var s: String? = "Foo"<br>print(s?.characters)<br><br>The result indicates that s?.characters is indeed an Optional instance, indicating that s?.characters.count should be illegal.<br><br>Why is s?.characters.count a legal expression?<br><br><br>--<br>Stephen Schaub
</div></div>