English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Solution to Chinese Character Garbled Characters When Reading txt File in iOS

I. Scenario Description:

 The background provides a txt file with encoding utf-8In the Xcode development environment on Mac, when reading the content of a txt file, Chinese characters will appear as garbled, while English characters do not have this issue. 

Second, Try the Solution:

Modify the encoding format, tried NSUTF16StringEncoding, NSUTF8StringEncoding, NSASCIIStringEncoding encoding, and other issues sometimes appear as Chinese character garbled, and sometimes as utf-8Unable to open file issue, and the ultimate problem was not resolved. 

Three, Guess the Reason:

The txt file is created on the Windows computer, which may be related to the environment, and secondly, encoding issues.

Four, Solution:

First step: Create a txt file on Mac, and copy and paste the content of the txt file copied from the window into it.

Second step: Encoding format, use NSUTF16StringEncoding 

Five, 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 {
}

That is the solution to the problem of Chinese character garbled when reading txt files in iOS, I hope it will be helpful to everyone's learning, and I also hope everyone will support the Shouting Tutorial more.

Statement: The content of this article is from the network, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not undergo人工 editing, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (when sending an email, please replace # with @ to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

Elasticsearch Tutorial