NSString stringWithContentsOfFile:usedEncoding:error:

Fritz Anderson fritza at manoverboard.org
Fri Jun 17 08:59:41 PDT 2005


On 17 Jun 2005, at 10:30 AM, Jerome Chan wrote:

> What is the equivalent of NSString  
> stringWithContentsOfFile:usedEncoding:error: under 10.3.9? I need  
> to read in a file but I'm not sure what the encoding is. How do you  
> deal with this?

You've no doubt discovered that encoding-sniffing is new with Tiger.

Before this, people muddled through as best they could. You can load  
an NSData with the contents of the file and sniff the first four  
bytes of the file for Unicode byte-order markers (a file can still be  
UTF without a BOM). You can sniff the whole contents for bit 7 being  
set, and if it never is, pick ASCII.

After that, you guess based on your market. Mac Roman encoding is  
often the safest 8-bit encoding, though UTF-8 is taking over. ISO  
Latin-1, if you're dealing with Windows-origin text that isn't Unicode.

When you're ready to throw the dice, use -[NSString  
initWithData:encoding:] and watch the fun.

     -- F




More information about the MacOSX-dev mailing list