ios-Read txt file Chinese garbled

Source: Internet
Author: User

First, Scenario Description:

Background to a TXT file, encoding is Utf-8, in the MAC computer Xcode development environment Read TXT file content, Chinese characters will appear garbled, the English is not garbled this situation.

Second, try to solve the method:

Modify the encoding format, try the nsutf16stringencoding,nsutf8stringencoding,nsasciistringencoding encoding, etc., the problem is sometimes Chinese garbled, sometimes utf-8 can not open the file problem, The final question was not solved.

Third, the reason for speculation:

TXT files are created from the Windows computer and may have an environment-related, second, coding problem.

Three, the solution:

The first step: Create a new TXT file on your Mac, copy and paste the contents of the TXT file from the window.

Part II: Encoding format, using nsutf16stringencoding

Four, the code:

  Nserror *error;   *path = [[NSBundle mainbundle]pathforresource:@ "body" ofType:@ "txt " ];   *content = [NSString stringwithcontentsoffile:path encoding:nsasciistringencoding error:&error];     if (Error) {        NSLog (@ "====%@", error.localizeddescription);     Else {}    

ios-Read txt file Chinese garbled

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.