Sometimes your system may send a message saying nsstring is initwithdata error. There can be several reasons for this problem.
As Peter said, the content-of-type header is hardly a “hint” as to what to expect from articles submitted with content. On the server side, you can pretty much specify content types and send any byte pattern that might not be valid.
I had exactly the same problem with invalid UTF-8 data containing (Latin-1) iso-8859-1 characters (French accents).
Wikipedia on whether to read utf-8, understand that version, and fix selection errors. Done,
en the implementation of InitWithData:encoding: just
nsstring returns nil if an error occurs. (as opposed to Java in terms of instances using Peter's solution to restore normal characters)
Converting UTF-8 data to Was Latin-1 does not satisfy me.(All UTF-8 characters are unbalanced for one rare Latin 1 character)
The best option, of course, would be a third-party server plan, but then I'm not responsible...
So I looked around and found a fluid that uses the standard GNU libiconv C (available for OSX and iOS).The principle is to useremoving iconv for invalid characters reduced to UTF-8 (i.e. "preted" becomes "prt")
here is a sample code, exactly the same command line
iconv -m -f -t UTF-8 utf-8 invalid.txt > clean.txt
#include "icon.h"- (NSData *) cleanUTF8: (NSData *) data iconv_t cd = iconv_open("UTF-8", // "utf-8"); convert from UTF-8 to UTF-8 integer = 1; iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, &one); character // throw size_t is invalid inbytesleft, outbytesleft; inbytesleft = outbytesleft = data.length; char *inbuf (because it implies *)data.bytes; Because = *outbuf 7. malloc(sizeof(char) data.length); char *outptr equals outbuf; if (icon(cd, &inbytesleft, &inbuf, &outptr, &outbytesleft) == (size_t)-1) NSLog(@"this shouldn't be happening, seriously"); returns null; NSData * result = [NSData dataWithBytes: outbuf length: data.- length outbytesleft]; iconv_close (cd); (outbuff); free return of the result;
Note that buzzwords also allow fallback methods and :
With iconv_set_fallbacks, which you only use to fix Unicode errors, you'll probably use a fallback or higher quality to try a different encoding.In the trial version, I managed toGo to LATIN-1, where I failed Utf-8, resulting in 99% positive conversions. See iconv source code to understand.
Ars Scholae Palatinae
Signed: June 25, 2010