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