如何在NSString中正确编码Unicode字符? [英] How do I properly encode Unicode characters in my NSString?

查看:125
本文介绍了如何在NSString中正确编码Unicode字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了许多字符串,将它们连接成CSV格式,然后将字符串作为附件发送电子邮件。

I create a number of strings, concatenate them together into CSV format, and then email the string as an attachment.

当这些字符串仅包含ASCII字符时,将生成CSV文件并通过电子邮件发送。当我包含非ASCII字符时,结果字符串变得格式错误,并且未正确创建CSV文件。 (电子邮件视图显示附件,但不会发送。)

When these strings contain only ASCII characters, the CSV file is built and emailed properly. When I include non-ASCII characters, the result string becomes malformed and the CSV file is not created properly. (The email view shows an attachment, but it is not sent.)

例如,这有效:

uncle bill's house of pancakes

但这不是(请注意卷曲撇号):

But this doesn't (note the curly apostrophe):

uncle bill’s house of pancakes



问题



如何正确创建和编码最终字符串,以便包含所有有效的unicode字符和结果字符串是否正确形成?

Question

How do I create and encode the final string properly so that all valid unicode characters are included and the result string is formed properly?


  • 字符串是通过UITextField创建,然后写入并从核心数据存储读取。

  • The strings are created via a UITextField and then are written to and then read from a Core Data store.

这表明问题在于初始创建和编码字符串: NSString unicode编码问题

This suggests that the problem lies in the initial creation and encoding of the string: NSString unicode encoding problem

我不想这样做: 从objective-c中的NSString中删除非ASCII字符

I don't want to have to do this: remove non ASCII characters from NSString in objective-c

编写字符串并从数据存储中读取数据。字符串在应用程序的表视图中正确显示(单独)。只有在为电子邮件附件连接字符串时才会出现此问题。

The strings are written and read to/from the data store fine. The strings display properly (individually) in the app's table views. The problem only manifests when concatenating the strings together for the email attachment.

我将我的字符串连接在一起如下:

I concatenate my strings together like this:

[reportString appendFormat:@"%@,", category];
[reportString appendFormat:@"%@,", client];
[reportString appendFormat:@"%@\n", detail];
etc.

用无聊的引号替换卷曲引号使其有效,但我不知道想要这样做:

Replacing curly quotes with boring quotes makes it work, but I don't want to do it this 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];
}

修改:
电子邮件是已发送:

The email is sent:

    NSString *attachment = [self formatReportCSV];
    [picker addAttachmentData:[attachment dataUsingEncoding:NSStringEncodingConversionAllowLossy] mimeType:nil fileName:@"MyCSVFile.csv"];

其中 formatReportCSV 是连接和返回csv字符串。

where formatReportCSV is the function that concatenates and returns the csv string.

推荐答案

您似乎遇到了字符串编码问题。如果没有看到你的核心数据模型是什么样的,我认为这个问题归结为下面代码重现的问题。

You seem to be running across a string encoding issue. Without seeing what your Core Data model looks like, I'd assume the issue boils down to the issue reproduced by the code below.

NSString *string1 = @"Uncle bill’s house of pancakes.";
NSString *string2 = @" Appended with some garbage's stuff.";
NSMutableString *mutableString = [NSMutableString stringWithString: string1];
[mutableString appendString: string2];
NSLog(@"We got: %@", mutableString);
// We got: Uncle bill’s house of pancakes. Appended with some garbage's stuff.

NSData *storedVersion = [mutableString dataUsingEncoding: NSStringEncodingConversionAllowLossy];
NSString *restoredString = [[NSString alloc] initWithData: storedVersion encoding: NSStringEncodingConversionAllowLossy];
NSLog(@"Restored string with NSStringEncodingConversionAllowLossy: %@", restoredString);
// Restored string with NSStringEncodingConversionAllowLossy: 

storedVersion = [mutableString dataUsingEncoding: NSUTF8StringEncoding];
restoredString = [[NSString alloc] initWithData: storedVersion encoding: NSUTF8StringEncoding];
NSLog(@"Restored string with UTF8: %@", restoredString);
// Restored string with UTF8: Uncle bill’s house of pancakes. Appended with some garbage's stuff.

注意第一个字符串(使用ASCII编码)无法处理非ASCII的存在character(如果你使用 dataUsingEncoding:allowsLossyConversion:,第二个参数是 YES )。

Note how the first string (encoded using ASCII) couldn't handle the presence of the non-ASCII character (it can if you use dataUsingEncoding:allowsLossyConversion: with the second parameter being YES).

此代码应解决问题:

NSString *attachment = [self formatReportCSV];
[picker addAttachmentData:[attachment dataUsingEncoding: NSUTF8StringEncoding] mimeType:nil fileName:@"MyCSVFile.csv"];

注意:如果你需要处理非UTF8,你可能需要使用UTF16字符串编码之一像日语这样的语言。

Note: you may need to use one of the UTF16 string encodings if you need to handle non-UTF8 languages like Japanese.

这篇关于如何在NSString中正确编码Unicode字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆