简体   繁体   English

从NSAttributedString到NSString UTF-8

[英]NSAttributedString to NSString UTF-8

My code is something like: 我的代码是这样的:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[textView textStorage]];
NSString *text = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
[xml appendFormat:@">%@", text]; 

If I have NSASCIIStringEncoding string is ok. 如果我有NSASCIIStringEncoding字符串是可以的。 When I change NSASCIIStringEncoding to NSUTF8StringEncoding it returns null.But I need UTF-8 because my XML file where I'm appending the string is in UTF-8. 当我将NSASCIIStringEncoding更改为NSUTF8StringEncoding时,它返回null。但是我需要UTF-8,因为我要附加字符串的XML文件位于UTF-8中。 Any way how to do that? 有什么办法吗? Thx for reply. 谢谢。

If textView is a UITextView, and xml is a NSMutableString, you should be able to replace that code with: 如果textView是UITextView,而xml是NSMutableString,则应该可以将该代码替换为:

[xml appendString: textView.text];

or if you need that > prefix: 或者如果您需要该>前缀:

[xml appendFormat:@">%@", textView.text];

NSString will keep track of the encodings. NSString将跟踪编码。 You only need to worry about the encoding later when you do something with xml (store it or send it through some web service). 使用xml (存储或通过某些Web服务发送)时,您只需要稍后担心编码。 And then you can simply use xml.UTF8String to get a regular C-string in UTF8 format. 然后,您可以简单地使用xml.UTF8String来获取UTF8格式的常规C字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM