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