<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">When you write `(s?.characters).count`, the parentheses are evaluated first; `(s?.characters)` gives an `String.CharacterView?`. Accessing the&nbsp;`String.CharacterView?`’s `count` property requires a `?`:&nbsp;`(s?.characters)?.count`. `s?.characters.count`, on the other hand, is applying chaining, which only gives an Optional at the end, intermediate properties don’t require a `?` unless they’re Optional themselves.<div class=""><br class=""><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Saagar Jha<br class=""><br class=""><br class=""></div>

</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Aug 1, 2016, at 11:17, Stephen Schaub &lt;<a href="mailto:sschaub@gmail.com" class="">sschaub@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">I understand that the String.characters property is not optional. However, I am puzzled as to why<div class=""><br class=""></div><div class="">s?.characters.count</div><div class=""><br class=""></div><div class="">is legal, but</div><div class=""><br class=""></div><div class="">(s?.characters).count</div><div class=""><br class=""></div><div class="">is not. This seems counterintuitive. Can someone explain the logic or rules being used here?</div><div class=""><br class=""></div><div class="">Stephen</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Mon, Aug 1, 2016 at 2:09 PM, Saagar Jha <span dir="ltr" class="">&lt;<a href="mailto:saagar@saagarjha.com" target="_blank" class="">saagar@saagarjha.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><br class=""><div class="">
<div style="letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; word-wrap: break-word;" class="">Saagar Jha<br class=""><br class="">This isn’t quite how optional chaining in Swift works; see the&nbsp;<a href="https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html" target="_blank" class="">Swift Programming Guide</a>, specifically “Linking Multiple Levels of Chaining". Basically, `s?.characters.count` works because `s.characters` isn’t Optional. You only use ? on properties that are Optional.</div>

</div>
<br class=""><div class=""><blockquote type="cite" class=""><div class=""><div class="h5"><div class="">On Aug 1, 2016, at 10:26, Stephen Schaub via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt; wrote:</div><br class=""></div></div><div class=""><div class=""><div class="h5"><div dir="ltr" class="">With optional chaining, if I have a Swift variable<br class=""><br class="">&nbsp; &nbsp; var s: String?<br class=""><br class="">s might contain nil, or a String wrapped in an Optional. So, I tried this to get its length:<br class=""><br class="">&nbsp; &nbsp; let count = s?.characters?.count ?? 0<br class=""><br class="">However, the compiler wants this:<br class=""><br class="">&nbsp; &nbsp; let count = s?.characters.count ?? 0<div class=""><br class=""></div><div class="">or this:</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp;&nbsp;let count = (s?.characters)?.count ?? 0<br class=""><br class="">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 class=""><br class="">So, I dug a little further and tried this in the playground:<br class=""><br class="">var s: String? = "Foo"<br class="">print(s?.characters)<br class=""><br class="">The result indicates that s?.characters is indeed an Optional instance, indicating that s?.characters.count should be illegal.<br class=""><br class="">Why is s?.characters.count a legal expression?<br class=""><br class=""><br class="">--<br class="">Stephen Schaub
</div></div></div></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></blockquote></div><br class=""><br clear="all" class=""><div class=""><br class=""></div>-- <br class=""><div class="gmail_signature" data-smartmail="gmail_signature">Stephen Schaub</div>
</div>
</div></blockquote></div><br class=""></div></body></html>