Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSString from text file with unknown encoding

I'm trying to show content of text file with unknown encoding according to Apple's documentation:

Try stringWithContentsOfFile:usedEncoding:error: or initWithContentsOfFile:usedEncoding:error: (or the URL-based equivalents). These methods try to determine the encoding of the resource, and if successful return by reference the encoding used.

If (1) fails, try to read the resource by specifying UTF-8 as the encoding.

If (2) fails, try an appropriate legacy encoding. "Appropriate" here depends a bit on circumstances; it might be the default C string encoding, it might be ISO or Windows Latin 1, or something else, depending on where your data is coming from.

This is not always working. Is there more reliable ways to detect encoding?

like image 246
Daniil Rumyantsev Avatar asked Aug 29 '26 08:08

Daniil Rumyantsev


1 Answers

You should use NSAttributedString which can detect encoding. After long time testing different solutions, I use that:

NSError *error;
NSDictionary *options = [NSDictionary dictionary];
NSDictionary *attributes;
NSAttributedString *theString = [[NSAttributedString alloc] initWithURL:fileURL options:options documentAttributes:&attributes error:&error];
NSInteger detectedEncoding = [[attributes objectForKey:@"CharacterEncoding"] integerValue];

I tested many files from many sources/environment, and it seem to be efficient (thus you should check whether error is nil or not). For a plain csv file exported from Excel, I get this attributes dictionary (30 value means NSMacOSRomanStringEncoding:

{
    CharacterEncoding = 30;
    DocumentType = NSPlainText;
    UTI = "public.plain-text";
}
like image 102
Denis Avatar answered Aug 31 '26 22:08

Denis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!