How to convert Unicode strings (\u00e2, etc) into NSString for display?

前端 未结 3 1429
日久生厌
日久生厌 2020-12-06 21:08

I am trying to support arbitrary unicode from a variety of international users. They have already put a bunch of data into sqlite databases on their iPhones, and now I want

相关标签:
3条回答
  • 2020-12-06 21:40

    [...] there is no built-in Cocoa methods to convert [...]. Is this correct?

    It's not correct.

    You might be interested in CFStringTransform and it's capabilities. It is a full blown ICU transformation engine, which can (also) perform your requested transformation.

    See Using Objective C/Cocoa to unescape unicode characters, ie \u1234

    0 讨论(0)
  • 2020-12-06 21:43

    All NSStrings are Unicode.

    The problem with the “Frank\u00e2\u0080\u0099s iPad” data isn't that it's Unicode; it's that it's escaped to ASCII. “Frank’s iPad” is valid Unicode in any UTF, and is what you need.

    So, you need to see whether the database is returning the data escaped or the PHP layer is escaping it at some point. If either of those is the case, fix it if you can; the PHP resource should return UTF-8/16/32. Only if that approach fails should you seek to unescape the string on the Cocoa side.

    You're correct that there is no built-in way to unescape the string in Cocoa. If you get to that point, see if you can find some open-source code to do it; if not, you'll need to do it yourself, probably using NSScanner.

    0 讨论(0)
  • 2020-12-06 21:48

    Check that your web service response has Content type and charset. Also that xml has encoding specified. In PHP you need to add the following before printing XML:

    header('Content-type: text/xml; charset=UTF-8'); print '<?xml version="1.0" encoding="UTF-8"?>';

    I guess there is just no encoding specified.

    0 讨论(0)
提交回复
热议问题