Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In general, you can’t. However, you can quite reliably identify UTF-8 files – if a file is valid UTF-8, it’s not very likely that it’s supposed to be any other encoding (except if all the bytes are in the ASCII range, in which case any “extended ASCII” encoding, including UTF-8, will give you the same result). All Unicode encodings also have an optional <a href="http://en.wikipedia.org/wiki/Byte-order_mark" rel="noreferrer">BOM</a> which identifies them. So a reasonable approach would be:</p> <ul> <li>Look for a valid BOM. If there is one, use the appropriate encoding.</li> <li>Otherwise, try to interpret it as UTF-8. You can do this by calling <code>initWithData:data encoding:NSUTF8StringEncoding</code> and checking if the result is non-nil.</li> <li>If that fails, use a default 8-bit encoding, such as <code>-[NSString defaultCStringEncoding]</code> (which provides a locale-appropriate guess).</li> </ul> <p>It <em>is</em> possible to try to improve the guess in the last step by trying various different encodings and choosing the one which has fewest sequences of letters with junk in the middle, where “junk” is any character that’s not a letter, space or common punctuation mark. This would significantly increase complexity while not actually being reliable.</p> <p>In short, to be able to handle all available encodings you need to do what TextEdit does: shunt the decision over to the user.</p> <p>Oh, one more thing: as of 10.5, the encoding is often stored with a file in the undocumented com.apple.TextEncoding extended attribute. If you open a file with <code>+[NSString stringWithContentsOfFile:]</code> or similar, this will automatically be used if present.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload