<div dir="ltr">Got it, I think! I didn't understand that the chaining of intermediate properties worked that way. <div><br></div><div>Thanks very much for the explanation. <br><div><br></div><div>Stephen</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Aug 1, 2016 at 2:25 PM, Saagar Jha <span dir="ltr"><<a href="mailto:saagar@saagarjha.com" target="_blank">saagar@saagarjha.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">When you write `(s?.characters).count`, the parentheses are evaluated first; `(s?.characters)` gives an `String.CharacterView?`. Accessing the `String.CharacterView?`’s `count` property requires a `?`: `(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><span class="HOEnZb"><font color="#888888"><br><div>
<div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word">Saagar Jha<br><br><br></div>
</div></font></span><div><div class="h5">
<br><div><blockquote type="cite"><div>On Aug 1, 2016, at 11:17, Stephen Schaub <<a href="mailto:sschaub@gmail.com" target="_blank">sschaub@gmail.com</a>> wrote:</div><br><div><div dir="ltr">I understand that the String.characters property is not optional. However, I am puzzled as to why<div><br></div><div>s?.characters.count</div><div><br></div><div>is legal, but</div><div><br></div><div>(s?.characters).count</div><div><br></div><div>is not. This seems counterintuitive. Can someone explain the logic or rules being used here?</div><div><br></div><div>Stephen</div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Aug 1, 2016 at 2:09 PM, Saagar Jha <span dir="ltr"><<a href="mailto:saagar@saagarjha.com" target="_blank">saagar@saagarjha.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div>
<div style="letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word">Saagar Jha<br><br>This isn’t quite how optional chaining in Swift works; see the <a href="https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html" target="_blank">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><div><blockquote type="cite"><div><div><div>On Aug 1, 2016, at 10:26, Stephen Schaub via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</div><br></div></div><div><div><div><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></div></div>
_______________________________________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div data-smartmail="gmail_signature">Stephen Schaub</div>
</div>
</div></blockquote></div><br></div></div></div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Stephen Schaub</div>
</div>