If you're tired of CFURLCreateStringByAddingPercentEscapes()
, try this.
I didn't write a Swift version, but it should be easy for you to adapt.
It is both funny and ironic that until today, in 2025, the standard library of Apple’s Cocoa ecosystem still lacks out-of-the-box but versatile APIs that can handle these most basic (and important) things "correctly", and vary with different common scenarios.
Discussion:
Q: Why not use
.alphanumericCharacterSet
+"-_.!~*'()"
(byaddCharactersInString
)A: Although it works, the way it works is somehow weird.
On Stack Overflow, many people have pointed out that
.alphanumericCharacterSet
is actually a HUGE map that includes international character (Unicode) and not just ASCII.Take a look at
[NSCharacterSet.alphanumericCharacterSet bitmapRepresentation]
. It covers many Unicode characters.This leads me to believe that
.alphanumericCharacterSet
as "AllowedCharacters" is not the intended use.