How can I escape unicode characters in a NSString?

前端 未结 2 1411
面向向阳花
面向向阳花 2020-12-18 04:50

When I store a NSString inside some NSDictionary and log that dictionary to the console like this:

NSString *someString = @\"Münster\";  
NSDictionary *som         


        
2条回答
  •  失恋的感觉
    2020-12-18 05:27

    NSDictionary *someDict = [ NSDictionary dictionaryWithObjectsAndKeys: 
        someString, @"thestring" ];
    

    Don't forget the nil sentinel. ;)

    The console output looks like this:

    unicode_test[3621:903] someDict:
    {
        thestring = "M\U00fcnster";
    }
    

    with the string's unicode character escaped.

    They're all Unicode characters.

    Is there any method to convert a NSString to this escaped representation?

    That's the dictionary (or some private method of NSPropertyListSerialization or private function of CFPropertyList) doing that, not the string. The \U sequence in that output is part of the OpenStep plist format. If you output the plist as XML using NSPropertyListSerialization, you'll find the ü (currently) encoded as naked UTF-8.

    As far as I know, there is no built-in method, public or private, that will do the same escaping for you on a string alone. The closest thing is the strvis function, but that works byte-by-byte; it doesn't understand Unicode or UTFs.

提交回复
热议问题