IOS-read txt file Chinese garbled characters, ios-read txt garbled characters

Source: Internet
Author: User

IOS-read txt file Chinese garbled characters, ios-read txt garbled characters

I. Scenario Description:

A txt file is encoded in UTF-8 in the background. It is read from the txt file in the MACOs Xcode development environment. Chinese characters may be garbled and English characters are not garbled.

Ii. Try to solve the problem:

After modifying the encoding format, I tried NSUTF16StringEncoding, NSUTF8StringEncoding, and NSASCIIStringEncoding encoding. The problem sometimes occurs when Chinese characters are garbled, sometimes UTF-8 files cannot be opened, and the final problem cannot be solved.

Iii. Cause of speculation:

The txt file is created on the Windows computer and may be related to the environment. Second, the encoding problem.

Iii. solution:

Step 1: Create a txt file on the Mac and copy and paste the txt file from the window.

Part 2: encoding format, using NSUTF16StringEncoding

4. Code:

  NSError *error;  NSString *path = [[NSBundle mainBundle]pathForResource:@"body" ofType:@"txt"];  NSString *content = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:&error];    if (error) {        NSLog(@"====%@",error.localizedDescription);    } else {}    

 

 

Related Article

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.