<div dir="ltr"><div class="markdown-here-wrapper" style=""><p style="margin:0px 0px 1.2em!important">It would be nice if for each enum case, there was a computed property returning an optional. Then you could do:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="hljs language-swift" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline;white-space:pre;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;overflow-x:auto;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,248)"><span class="hljs-class"><span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">enum</span> <span class="hljs-title" style="color:rgb(153,0,0);font-weight:bold;color:rgb(68,85,136);font-weight:bold">FooBar</span> </span>{
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">case</span> foo(<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Int</span>)
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">case</span> bar(<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Int</span>)
}
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> x = <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">FooBar</span>.foo(<span class="hljs-number" style="color:rgb(0,128,128)">42</span>)
x.foo <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">//=> Optional(42)</span>
x.bar <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">//=> nil</span>
</code></pre>
<p style="margin:0px 0px 1.2em!important">This feature has been discussed many times, but each time it was considered “out of scope”.</p>
<div title="MDH:SXQgd291bGQgYmUgbmljZSBpZiBmb3IgZWFjaCBlbnVtIGNhc2UsIHRoZXJlIHdhcyBhIGNvbXB1
dGVkIHByb3BlcnR5IHJldHVybmluZyBhbiBvcHRpb25hbC4gVGhlbiB5b3UgY291bGQgZG86PGRp
dj48YnI+PC9kaXY+PGRpdj5gYGBzd2lmdDwvZGl2PjxkaXY+ZW51bSBGb29CYXIgezwvZGl2Pjxk
aXY+Jm5ic3A7ICZuYnNwOyBjYXNlIGZvbyhJbnQpPC9kaXY+PGRpdj4mbmJzcDsgJm5ic3A7IGNh
c2UgYmFyKEludCk8L2Rpdj48ZGl2Pn08L2Rpdj48ZGl2Pjxicj48L2Rpdj48ZGl2PmxldCB4ID0g
Rm9vQmFyLmZvbyg0Mik8L2Rpdj48ZGl2PnguZm9vICZuYnNwOy8vPSZndDsgT3B0aW9uYWwoNDIp
PC9kaXY+PGRpdj54LmJhciAmbmJzcDsvLz0mZ3Q7IG5pbDwvZGl2PjxkaXY+YGBgPC9kaXY+PGRp
dj48YnI+PC9kaXY+PGRpdj5UaGlzIGZlYXR1cmUgaGFzIGJlZW4gZGlzY3Vzc2VkIG1hbnkgdGlt
ZXMsIGJ1dCBlYWNoIHRpbWUgaXQgd2FzIGNvbnNpZGVyZWQgIm91dCBvZiBzY29wZSIuPC9kaXY+" style="height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-size:0em;padding:0;margin:0"></div></div></div>