[swift-users] Cast CFString to String

Dennis Weissmann dennis at dennisweissmann.me
Tue Dec 5 03:09:53 CST 2017


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: https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html
        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: https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html
        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: https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html
        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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20171205/4705f60e/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: UTITest.playground.zip
Type: application/zip
Size: 2039 bytes
Desc: not available
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20171205/4705f60e/attachment.zip>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20171205/4705f60e/attachment-0001.html>


More information about the swift-users mailing list