ios - How do I properly encode Unicode characters in my NSString? -


problem statement

i create number of strings, concatenate them csv format, , email string attachment.

when these strings contain ascii characters, csv file built , emailed properly. when include non-ascii characters, result string becomes malformed , csv file not created properly. (the email view shows attachment, not sent.)

for instance, works:

uncle bill's house of pancakes 

but doesn't (note curly apostrophe):

uncle bill’s house of pancakes 

question

how create , encode final string valid unicode characters included , result string formed properly?

notes

string processing code

i concatenate strings this:

[reportstring appendformat:@"%@,", category]; [reportstring appendformat:@"%@,", client]; [reportstring appendformat:@"%@\n", detail]; etc. 

replacing curly quotes boring quotes makes work, don't want way:

- (nsmutablestring *)cleanstring:(nsstring *)activity {     nsstring *temp1 = [activity stringbyreplacingoccurrencesofstring:@"’" withstring:@"'"];     nsstring *temp2 = [temp1 stringbyreplacingoccurrencesofstring:@"‘" withstring:@"'"];     nsstring *temp3 = [temp2 stringbyreplacingoccurrencesofstring:@"”" withstring:@"\""];     nsstring *temp4 = [temp3 stringbyreplacingoccurrencesofstring:@"“" withstring:@"\""];     return [nsmutablestring temp4]; } 

edit: email sent:

    nsstring *attachment = [self formatreportcsv];     [picker addattachmentdata:[attachment datausingencoding:nsstringencodingconversionallowlossy] mimetype:nil filename:@"mycsvfile.csv"]; 

where formatreportcsv function concatenates , returns csv string.

you seem running across string encoding issue. without seeing core data model looks like, i'd assume issue boils down issue reproduced code below.

nsstring *string1 = @"uncle bill’s house of pancakes."; nsstring *string2 = @" appended garbage's stuff."; nsmutablestring *mutablestring = [nsmutablestring stringwithstring: string1]; [mutablestring appendstring: string2]; nslog(@"we got: %@", mutablestring); // got: uncle bill’s house of pancakes. appended garbage's stuff.  nsdata *storedversion = [mutablestring datausingencoding: nsstringencodingconversionallowlossy]; nsstring *restoredstring = [[nsstring alloc] initwithdata: storedversion encoding: nsstringencodingconversionallowlossy]; nslog(@"restored string nsstringencodingconversionallowlossy: %@", restoredstring); // restored string nsstringencodingconversionallowlossy:   storedversion = [mutablestring datausingencoding: nsutf8stringencoding]; restoredstring = [[nsstring alloc] initwithdata: storedversion encoding: nsutf8stringencoding]; nslog(@"restored string utf8: %@", restoredstring); // restored string utf8: uncle bill’s house of pancakes. appended garbage's stuff. 

note how first string (encoded using ascii) couldn't handle presence of non-ascii character (it can if use datausingencoding:allowslossyconversion: second parameter being yes).

this code should fix issue:

nsstring *attachment = [self formatreportcsv]; [picker addattachmentdata:[attachment datausingencoding: nsutf8stringencoding] mimetype:nil filename:@"mycsvfile.csv"]; 

note: may need use 1 of utf16 string encodings if need handle non-utf8 languages japanese.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -