<blockquote type="cite"><div><span><div><div><div dir="ltr"><div><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></div></div></div></div></span></div></blockquote><div><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81"><br></span></div><div><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">See print</span><span style="font-variant-ligatures: no-common-ligatures;">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">s</span><span style="font-variant-ligatures: no-common-ligatures;">?.</span><span style="font-variant-ligatures: no-common-ligatures">characters</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures">count</span><span style="font-variant-ligatures: no-common-ligatures;">) — you get the optional despite count not being defined as optional</span></div><div> </div><div><span style="font-size: 12px;">Also, try this:</span></div><div><span style="font-size: 12px;"><br></span></div><div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">struct</span><span style="font-variant-ligatures: no-common-ligatures"> Foo {</span></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">let</span><span style="font-variant-ligatures: no-common-ligatures"> bar: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Int</span></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures">}</span></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><span style="font-variant-ligatures: no-common-ligatures"></span><br></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">var</span><span style="font-variant-ligatures: no-common-ligatures"> foo: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">Foo</span><span style="font-variant-ligatures: no-common-ligatures">? = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">Foo</span><span style="font-variant-ligatures: no-common-ligatures">(bar: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">42</span><span style="font-variant-ligatures: no-common-ligatures">)</span></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);"><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">(</span><span style="font-variant-ligatures: no-common-ligatures">foo</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">?.</span><span style="font-variant-ligatures: no-common-ligatures">bar</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">)</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"><br></span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">Then try this:</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"><br></span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">struct</span><span style="font-variant-ligatures: no-common-ligatures"> Foo {</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">let</span><span style="font-variant-ligatures: no-common-ligatures"> bar: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Int</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">let</span><span style="font-variant-ligatures: no-common-ligatures"> baz: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Int</span><span style="font-variant-ligatures: no-common-ligatures">?</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures">}</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><span style="font-variant-ligatures: no-common-ligatures"></span><br></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">var</span><span style="font-variant-ligatures: no-common-ligatures"> foo: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">Foo</span><span style="font-variant-ligatures: no-common-ligatures">? = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">Foo</span><span style="font-variant-ligatures: no-common-ligatures">(bar: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">42</span><span style="font-variant-ligatures: no-common-ligatures">, baz: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">69</span><span style="font-variant-ligatures: no-common-ligatures">)</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);"><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">(</span><span style="font-variant-ligatures: no-common-ligatures">foo</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">?.</span><span style="font-variant-ligatures: no-common-ligatures">bar</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">)</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">
</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);"><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">(</span><span style="font-variant-ligatures: no-common-ligatures">foo</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">?.</span><span style="font-variant-ligatures: no-common-ligatures">baz</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">?)</span></p></div><div><span style="font-variant-ligatures: no-common-ligatures; color: #000000"><br></span></div><div><span style="font-variant-ligatures: no-common-ligatures; color: #000000"><br></span></div><div><span style="font-variant-ligatures: no-common-ligatures; color: #000000">This may give you some ideas.</span></div><div><span style="font-variant-ligatures: no-common-ligatures; color: #000000"><br></span></div><div><span style="font-variant-ligatures: no-common-ligatures; color: #000000">Best regards,</span></div><div><span style="font-variant-ligatures: no-common-ligatures; color: #000000">Rimantas</span></div><div><span style="font-variant-ligatures: no-common-ligatures; color: #000000"><br></span></div>