将字符集名称转换为 NSStringEncoding
给定一个字符集字符串,例如“utf-8”,“iso-8859-1”,“us-ascii”等,是否有任何内置方法可以在Cocoa中获取适当的NSStringEncoding?
现在,我正在考虑构建一个 NSDictionary,其中包含映射到 NSStringEncoding 的名称的规范化版本,然后拥有一个以相同方式规范化输入的查找机制。但是,给定字符串“UTF-8”等,真的没有办法获得 NSUTF8StringEncoding 吗?
Given a charset string, such as "utf-8", "iso-8859-1", "us-ascii" etc, is there any built-in way to get the appropriate NSStringEncoding in Cocoa?
Right now I'm looking at just building a NSDictionary containing a canonicalized version of the name mapped to the NSStringEncoding, then having a lookup mechanism that canonicalizes the input in the same way. But is there really no way to get NSUTF8StringEncoding given the string "UTF-8", etc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你必须通过CoreFoundation。
首先将 IANA Char 集转换为 CFStringEncoding 值(与 NSStringEncoding 不同),然后将 CFStringEncoding 转换为 NSStringEncoding。
You have to go through CoreFoundation.
First converting the IANA Char set to CFStringEncoding value (Which is not the same as NSStringEncoding) and then convert the CFStringEncoding to NSStringEncoding.
我认为没有任何内置方法可以实现这一目标。我认为你的建议 - 建立一个包含值的字典 - 是一个明智的方法。没有那么多编码,因此它应该非常简单,并且可能比尝试找到内置方法更快。
I don't think there is any built-in way of achieving this. I think what you suggest - building a dictionary with the values - is a sensible way to go here. There aren't that many encodings so it should be quite trivial, and probably quicker than trying to find a built-in way of doing it.