Skip to content

Strings

Ken Harris edited this page Oct 22, 2020 · 3 revisions

This method has some issues. The docs for this (and its associated constants) have been wrong a lot, so if you were confused before (pre-2019), go read it all again.

  1. Returns an optional, but it never returns nil, even for bogus inputs.

    Not really a problem, I guess, but annoying and stupid.

  2. Doesn't work for all characters.

    "ü".applyingTransform(.toUnicodeName, reverse: false)!
    > String = "\N{LATIN SMALL LETTER U WITH DIAERESIS}"
    

    Looks good so far, right? But for some characters, it just returns the receiver:

    "u".applyingTransform(.toUnicodeName, reverse: false)! 
    > String = "u"
    

    The docs say this is equivalent to kCFStringTransformToUnicodeName, whose docs (now) say this transliterates chars "other than printable ASCII". Ugh.

    What to do instead? Use CFStringTransform with transform "Any-Name". That function promises to accept "any valid ICU transform ID defined in the ICU User Guide for Transforms", and it actually works. As a bonus, it works with any macOS since 10.4, while applyingTransform() only goes back to 10.11. The downside is that it's a pain to convert your Swift.String to an NSMutableString (and range) for Foundation.

  3. Doesn't work for all characters, part 2.

    Even with CFStringTransform, in 10.14, U+FEFF fails. It should return "\N{ZERO WIDTH NO-BREAK SPACE}", but it returns "" (the empty string). You'll have to hardcode this character name, and check it by hand. I've checked every other codepoint, and this is the only one that fails.

    This seems to be fixed in 10.15. I don't think it was a problem in 10.13, either.

Clone this wiki locally