Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The NSString <code>-initWithData:encoding:</code> method returns <code>nil</code> if it fails, so you can try one encoding after another until you find one that converts. This doesn't guarantee that you'll convert all the characters correctly, but if your feed source isn't sending you correctly encoded XML, then you'll probably have to live with it.</p> <p>The basic idea is:</p> <pre><code>// try the most likely encoding NSString xmlString = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding]; if (xmlString == nil) { // try the next likely encoding xmlString = [[NSString alloc] initWithData:xmlData encoding:NSWindowsCP1252StringEncoding]; } if (xmlString == nil) { // etc... } </code></pre> <p>To be generic and robust, you could do the following until successful:</p> <p>1.) Try the encoding specified in the Content-Type header of the HTTP response (if any)</p> <p>2.) Check the start of the response data for a <a href="http://unicode.org/faq/utf_bom.html#BOM" rel="nofollow noreferrer">byte order mark</a> and if found, try the indicated encoding</p> <p>3.) Look at the first two bytes; if you find a whitespace character or '&lt;' paired with a nul/zero character, try UTF-16 (similarly, you can check the first four bytes to see if you have UTF-32)</p> <p>4.) Scan the start of the data looking for the <code>&lt;?xml ... ?&gt;</code> processing instruction and look for <code>encoding='something'</code> inside it; try that encoding.</p> <p>5.) Try some common encodings. Definitely check Windows Latin-1, Mac Roman, and ISO Latin-1 if your data source is in English.</p> <p>6.) If none of the above work, you could try removing all bytes greater than 127 (or substitute '?' or another ASCII character) and convert the data using the ASCII encoding.</p> <p>If you don't have an NSString by this point, you should fail. If you do have an NSString, you should look for the <code>encoding</code> declaration in the <code>&lt;?xml ... ?&gt;</code> processing instruction (if you didn't already in step 4). If it's there, you should convert the NSString back to NSData using that encoding; if it's not there, you should convert back using UTF-8 encoding.</p> <p>Also, the <code>CFStringConvertIANACharSetNameToEncoding()</code> and <code>CFStringConvertEncodingToNSStringEncoding()</code> functions can help get the NSStringEncoding that goes with the encoding name form the <code>Content-Type</code> header or the <code>&lt;?xml ... ?&gt;</code> processing instruction.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. 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