How does Any, Optional and Protocol work together in Swift?
<p>Can somebody explain why the first snippet is failing and the second is working?</p>

<ol>
<li><p>Error is optional type</p>

<pre><code>var error:Error? = NSError(domain: "MyDomain", code: 1001, userInfo: nil)
var anyError:Any = error
if let err = anyError as? Error
{
    print("success")
}
else {
    print("failure") // prints failure
}
</code></pre></li>
<li><p>NSError is optional type</p>

<pre><code>var error:NSError? = NSError(domain: "MyDomain", code: 1001, userInfo: nil)
var anyError:Any = error
if let err = anyError as? NSError
{
    print("success") // prints success
}
else {
    print("failure")
}
</code></pre></li>
</ol>