Hi swift-users,
I have found another weird behavior (IMO) and wanted to ask for the right way to handle this:
Imagine I want to switch over a Swift string which contains a UTI to map that UTI to an enum.
(A playground is attached for you to easily reproduce, this is tested with Xcode 9.1's included toolchain, also happens in projects)
I would expect the following to work:
import MobileCoreServices
enum MimeType {
case text
case unknown
public init(uti: String) {
// Source: System-Declared Uniform Type Identifiers
switch uti.lowercased() {
case kUTTypeText as String:
self = .text
default:
self = .unknown
}
}
}
The error I get here is
warning: UTITest.playground:8:26: warning: 'as' test is always true
case kUTTypeText as String:
^
error: UTITest.playground:8:14: error: expression pattern of type 'CFString' cannot match values of type 'String'
case kUTTypeText as String:
^~~~~~~~~~~
^~~~~~~~~~~
The only way I found to resolve this is to also import Foundation (which makes sense but is not really obvious).
Alright, that gives me this:
import MobileCoreServices
import Foundation
enum MimeType {
case text
case unknown
public init(uti: String) {
// Source: System-Declared Uniform Type Identifiers
switch uti.lowercased() {
case kUTTypeText as String:
self = .text
default:
self = .unknown
}
}
}
warning: UTITest.playground:8:26: warning: 'as' test is always true
case kUTTypeText as String:
^
error: UTITest.playground:8:14: error: 'CFString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?
case kUTTypeText as String:
^
as String
Uhm, okay? So let's do that:
enum MimeType {
case text
case unknown
public init(uti: String) {
// Source: System-Declared Uniform Type Identifiers
switch uti.lowercased() {
case kUTTypeText as String as String:
self = .text
default:
self = .unknown
}
}
}
As weird as it looks, it works ... My question is: Is this behavior intended?
Thanks!
- Dennis
UTITest.playground.zip (1.99 KB)