<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi swift-users,<div class=""><br class=""></div><div class="">I have found another weird behavior (IMO) and wanted to ask for the right way to handle this:</div><div class=""><br class=""></div><div class="">Imagine I want to switch over a Swift string which contains a UTI to map that UTI to an enum.</div><div class=""><br class=""></div><div class="">(A playground is attached for you to easily reproduce, this is tested with Xcode 9.1's included toolchain, also happens in projects)</div><div class=""><br class=""></div><div class="">I would expect the following to work:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""><span style="color: #c42275" class="">import</span> MobileCoreServices</div></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""><span style="color: #c42275" class="">enum</span> MimeType {</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">case</span> text</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">case</span> unknown</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">public</span> <span style="color: #c42275" class="">init</span>(uti: <span style="color: #703daa" class="">String</span>) {</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; color: rgb(19, 55, 255); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #1e9421" class="">// Source: </span><a href="https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html" class="">https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html</a></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">switch</span> uti.<span style="color: #3e1e81" class="">lowercased</span>() {</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #c42275" class="">case</span><span style="color: #000000" class=""> </span>kUTTypeText<span style="color: #000000" class=""> </span><span style="color: #c42275" class="">as</span><span style="color: #000000" class=""> </span>String<span style="color: #000000" class="">:</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">self</span> = .<span style="color: #31595d" class="">text</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">default</span>:</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">self</span> = .<span style="color: #31595d" class="">unknown</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class="">}</div></div><div class=""><br class=""></div><div class="">The error I get here is</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">warning: UTITest.playground:8:26: warning: 'as' test is always true</b></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class=""> case kUTTypeText as String:</b></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class=""> ^</b></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; min-height: 14px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class=""></b></span><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">error: UTITest.playground:8:14: error: expression pattern of type 'CFString' cannot match values of type 'String'</b></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class=""> case kUTTypeText as String:</b></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class=""> ^~~~~~~~~~~</b></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo;" class=""><b class=""> ^~~~~~~~~~~</b></div></div><div class="">The only way I found to resolve this is to also import Foundation (which makes sense but is not really obvious).</div><div class=""><br class=""></div><div class="">Alright, that gives me this:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""><span style="color: #c42275" class="">import</span> MobileCoreServices</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""><span style="color: #c42275" class="">import</span> Foundation</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""><span style="color: #c42275" class="">enum</span> MimeType {</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">case</span> text</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">case</span> unknown</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">public</span> <span style="color: #c42275" class="">init</span>(uti: <span style="color: #703daa" class="">String</span>) {</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; color: rgb(19, 55, 255); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #1e9421" class="">// Source: </span><a href="https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html" class="">https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html</a></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">switch</span> uti.<span style="color: #3e1e81" class="">lowercased</span>() {</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #c42275" class="">case</span><span style="color: #000000" class=""> </span>kUTTypeText<span style="color: #000000" class=""> </span><span style="color: #c42275" class="">as</span><span style="color: #000000" class=""> </span>String<span style="color: #000000" class="">:</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">self</span> = .<span style="color: #31595d" class="">text</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">default</span>:</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">self</span> = .<span style="color: #31595d" class="">unknown</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class="">}</div></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">warning: UTITest.playground:8:26: warning: 'as' test is always true</b></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class=""> case kUTTypeText as String:</b></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class=""> ^</b></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; min-height: 14px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class=""></b></span><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">error: UTITest.playground:8:14: error: 'CFString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?</b></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class=""> case kUTTypeText as String:</b></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class=""> ^</b></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class=""> as String</b></span></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Uhm, okay? So let's do that:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""><span style="color: #c42275" class="">enum</span> MimeType {</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">case</span> text</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">case</span> unknown</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">public</span> <span style="color: #c42275" class="">init</span>(uti: <span style="color: #703daa" class="">String</span>) {</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; color: rgb(19, 55, 255); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #1e9421" class="">// Source: </span><a href="https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html" class="">https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html</a></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">switch</span> uti.<span style="color: #3e1e81" class="">lowercased</span>() {</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #c42275" class="">case</span><span style="color: #000000" class=""> </span>kUTTypeText<span style="color: #000000" class=""> </span><span style="color: #c42275" class="">as</span><span style="color: #000000" class=""> </span>String<span style="color: #000000" class=""> </span><span style="color: #c42275" class="">as</span><span style="color: #000000" class=""> </span>String<span style="color: #000000" class="">:</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">self</span> = .<span style="color: #31595d" class="">text</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">default</span>:</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> <span style="color: #c42275" class="">self</span> = .<span style="color: #31595d" class="">unknown</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Fira Code"; background-color: rgb(255, 255, 255);" class="">}</div></div><div class=""><br class=""></div><div class="">As weird as it looks, it works ... My question is: Is this behavior intended?</div><div class=""><br class=""></div><div class="">Thanks!</div><div class=""><br class=""></div><div class="">- Dennis</div><div class=""></div></body></html>