<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><font color="#333333" face="helvetica, arial, freesans, clean, sans-serif"><span style="font-size:16px">Confused about </span></font><span style="color:rgb(153,0,0);font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:14px;font-weight:bold;line-height:20.3px;white-space:pre;background-color:rgb(248,248,248)">getAsNSString</span><span style="font-size:16px;color:rgb(51,51,51);font-family:helvetica,arial,freesans,clean,sans-serif"> from bullet point 5, used again in bullet point 8 — the function wont compile for me:</span></div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><span style="color:rgb(51,51,51);font-family:helvetica,arial,freesans,clean,sans-serif;font-size:16px">Check whether a value is </span><code style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">_ObjectiveCBridgeable</code><span style="color:rgb(51,51,51);font-family:helvetica,arial,freesans,clean,sans-serif;font-size:16px"> to a class, or conversely, that an object is </span><code style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">_ObjectiveCBridgeable</code><span style="color:rgb(51,51,51);font-family:helvetica,arial,freesans,clean,sans-serif;font-size:16px"> to a value type, and perform the bridging conversion if so:</span><br><pre style="color:rgb(51,51,51);overflow:auto;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:16px;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;min-height:306px;background-color:rgb(248,248,248)"><code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;padding:0.5em;margin:0px;border-radius:3px;border:0px;display:block;line-height:inherit;word-wrap:normal;min-height:auto"><span style="font-weight:bold">func</span> <span style="color:rgb(153,0,0);font-weight:bold">getAsString</span>&lt;T&gt;(value: T) -&gt; String? {
  <span style="font-weight:bold">return</span> value <span style="font-weight:bold">as</span>? String
}
<span style="font-weight:bold">func</span> <span style="color:rgb(153,0,0);font-weight:bold">getAsNSString</span>&lt;T&gt;(value: T) -&gt; NSString {
  <span style="font-weight:bold">return</span> value <span style="font-weight:bold">as</span>? NSString
}

getAsString(value: <span style="color:rgb(221,17,68)">&quot;string&quot;</span>) <span style="color:rgb(153,153,136);font-style:italic">// produces &quot;string&quot;: String</span>
getAsNSString(value: <span style="color:rgb(221,17,68)">&quot;string&quot;</span>) <span style="color:rgb(153,153,136);font-style:italic">// produces &quot;string&quot;: NSString</span>

<span style="font-weight:bold">let</span> ns = NSString(<span style="color:rgb(221,17,68)">&quot;nsstring&quot;</span>)
getAsString(value: ns) <span style="color:rgb(153,153,136);font-style:italic">// produces &quot;nsstring&quot;: String</span>
getAsNSString(value: ns) <span style="color:rgb(153,153,136);font-style:italic">// produces &quot;nsstring&quot;: NSString</span></code></pre></div></blockquote></div></div><div class="gmail_extra"><span style="color:rgb(51,51,51);font-family:helvetica,arial,freesans,clean,sans-serif;font-size:16px"><br></span></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><p style="color:rgb(51,51,51);font-family:helvetica,arial,freesans,clean,sans-serif;font-size:16px;margin:16px 0px">Drill through <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Optional</code>s. If an <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Optional</code> contains <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">some</code> value, it is extracted, and the cast is attempted on the contained value; the cast fails if the source value is <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">none</code> and the result type is not optional:</p><pre style="white-space:pre-wrap;color:rgb(51,51,51);overflow:auto;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:16px;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;min-height:126px;background-color:rgb(248,248,248)"><code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;padding:0.5em;margin:0px;border-radius:3px;border:0px;display:block;line-height:inherit;word-wrap:normal;min-height:auto"><span style="font-weight:bold">var</span> x: String? = <span style="color:rgb(221,17,68)">&quot;optional string&quot;</span>
getAsNSString(<span style="font-weight:bold">value</span>: x) <span style="color:rgb(153,153,136);font-style:italic">// produces &quot;optional string&quot;: NSString</span>
x = nil
<span style="color:rgb(153,0,0);font-weight:bold">getAsNSString</span>(<span style="font-weight:bold">value</span>: x) <span style="color:rgb(153,153,136);font-style:italic">// fails</span></code></pre></blockquote><div><pre class="" style="margin-top:0.5em;margin-bottom:0.5em;border:1px solid rgb(204,204,204);font-size:13px;line-height:1.5;overflow:auto;padding:1em;border-radius:3px;color:rgb(0,0,0);font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace;direction:ltr;word-wrap:normal;background-color:rgb(245,242,240)"><code class="" style="margin:0px;padding:0px;border:none;border-radius:3px;font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace;direction:ltr;word-spacing:normal;word-wrap:normal;line-height:1.5;background-color:transparent">$ /Applications/Xcode-7D1010.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.5 clang-703.0.31). Type :help for assistance.
  1&gt; import Foundation
  2&gt;
  3&gt; func getAsNSString&lt;T&gt;(value: T) -&gt; NSString {
  4.   return value as? NSString
  5. }
repl.swift:4:16: error: value of optional type &#39;NSString?&#39; not unwrapped; did you mean to use &#39;!&#39; or &#39;?&#39;?
  return value as? NSString
               ^
         (                 )!

  3&gt;

$ /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-04-12-a.xctoolchain/usr/bin/swift
Welcome to Apple Swift version 3.0-dev (LLVM 752e1430fc, Clang 3987718dae, Swift 36739f7b57). Type :help for assistance.
  1&gt; import Foundation
  2&gt;
  3&gt; func getAsNSString&lt;T&gt;(value: T) -&gt; NSString {
  4.   return value as? NSString
  5. }
error: repl.swift:4:16: error: value of optional type &#39;NSString?&#39; not unwrapped; did you mean to use &#39;!&#39; or &#39;?&#39;?
  return value as? NSString
               ^
         (                 )!</code></pre><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">I would think the function was meant to end <span style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;line-height:20.3px;white-space:pre;background-color:rgb(248,248,248)">-&gt; NSString?</span>, if not for the <span style="line-height:inherit;color:rgb(153,153,136);font-style:italic;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(248,248,248)">// fails </span>comment in bullet 8. Could you clarify?</p><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">– J.D. Healy</p></div></div>