iOS:使用特殊字符编码在标签中显示NSStrings的NSArray
我有几个iOS:使用特殊字符编码在标签中显示NSStrings的NSArray,ios,objective-c,character-encoding,Ios,Objective C,Character Encoding,我有几个NSStrings,我将它们添加到NSArray。字符串可能包含特殊字符。最后,我想将数组打印到UILabel 非常简单的代码(如果您认为我遗漏了什么,请告诉我): 字符串本身打印出来很好。但是,对于数组,它取决于输出方法,控制台是否为其显示正确的特殊字符或代码。标签仅打印代码,而不是实际字符。通过NSLog打印的结果如下所示: Buffer: Röckdöts Array: ( R\U00f6ckd\U00f6ts ) element 0: Röckdöts 鉴于标签上写着:
NSStrings
,我将它们添加到NSArray
。字符串可能包含特殊字符。最后,我想将数组打印到UILabel
非常简单的代码(如果您认为我遗漏了什么,请告诉我):
字符串本身打印出来很好。但是,对于数组,它取决于输出方法,控制台是否为其显示正确的特殊字符或代码。标签仅打印代码,而不是实际字符。通过NSLog
打印的结果如下所示:
Buffer: Röckdöts
Array: (
R\U00f6ckd\U00f6ts
)
element 0: Röckdöts
鉴于标签上写着:
R\U00f6ckd\U00f6ts
在添加到数组的过程中,我尝试使用stringWithUTF8String
,在将其分配到标签的过程中,我也尝试使用类似的编码,但没有改变结果:
// adding with UTF8 encoding
[marrSelectedStrings addObject:[NSString stringWithUTF8String:[strBuffer UTF8String]]];
// printing to label with UTF8 encoding
self.label.text = [NSString stringWithUTF8String:[[NSString stringWithFormat:@"%@", marrSelectedStrings] UTF8String]];
是否有一种简单的方法可以将正确的字符编码的数组打印到UILabel
上,而不是遍历数组并追加每个单词?试试这个方法
NSString * result = [[marrSelectedStrings valueForKey:@"description"] componentsJoinedByString:@""];
self.label.text = result;
试试这个
NSString * result = [[marrSelectedStrings valueForKey:@"description"] componentsJoinedByString:@""];
self.label.text = result;
像这样试试
NSMutableArray *marrSelectedStrings = [[NSMutableArray alloc] init];
[marrSelectedStrings addObject:strBuffer];
NSString *description = [marrSelectedStrings description];
if (description) {
const char *descriptionChar = [description cStringUsingEncoding:NSASCIIStringEncoding];
if (descriptionChar) {
NSString *prettyDescription = [NSString stringWithCString:descriptionChar encoding:NSNonLossyASCIIStringEncoding];
if (prettyDescription) {
description = prettyDescription;
}
}
}
NSLog(@"Array: %@", description);
像这样试试
NSMutableArray *marrSelectedStrings = [[NSMutableArray alloc] init];
[marrSelectedStrings addObject:strBuffer];
NSString *description = [marrSelectedStrings description];
if (description) {
const char *descriptionChar = [description cStringUsingEncoding:NSASCIIStringEncoding];
if (descriptionChar) {
NSString *prettyDescription = [NSString stringWithCString:descriptionChar encoding:NSNonLossyASCIIStringEncoding];
if (prettyDescription) {
description = prettyDescription;
}
}
}
NSLog(@"Array: %@", description);
非常感谢你!此外,该解决方案摆脱了丑陋的括号。非常感谢!另外,该解决方案摆脱了难看的括号。