将字符集名称转换为 NSStringEncoding

发布于 2024-09-03 07:34:51 字数 234 浏览 5 评论 0原文

给定一个字符集字符串,例如“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ˉ厌 2024-09-10 07:34:51

你必须通过CoreFoundation。

CFStringConvertIANACharSetNameToEncoding
CFStringConvertEncodingToNSStringEncoding

首先将 IANA Char 集转换为 CFStringEncoding 值(与 NSStringEncoding 不同),然后将 CFStringEncoding 转换为 NSStringEncoding。

NSString *encodingString = @"utf-8"
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef) encodingString));

You have to go through CoreFoundation.

CFStringConvertIANACharSetNameToEncoding
CFStringConvertEncodingToNSStringEncoding

First converting the IANA Char set to CFStringEncoding value (Which is not the same as NSStringEncoding) and then convert the CFStringEncoding to NSStringEncoding.

NSString *encodingString = @"utf-8"
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef) encodingString));
素食主义者 2024-09-10 07:34:51
NSStringEncoding utfEncoding = NSUTF8StringEncoding;
NSLog(@"%lu",utfEncoding);

NSString * utf8Name = @"UTF-8";
CFStringEncoding aEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)utf8Name);
utfEncoding = CFStringConvertEncodingToNSStringEncoding(aEncoding);
NSLog(@"%lu",utfEncoding);  
NSStringEncoding utfEncoding = NSUTF8StringEncoding;
NSLog(@"%lu",utfEncoding);

NSString * utf8Name = @"UTF-8";
CFStringEncoding aEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)utf8Name);
utfEncoding = CFStringConvertEncodingToNSStringEncoding(aEncoding);
NSLog(@"%lu",utfEncoding);  
浅忆 2024-09-10 07:34:51

我认为没有任何内置方法可以实现这一目标。我认为你的建议 - 建立一个包含值的字典 - 是一个明智的方法。没有那么多编码,因此它应该非常简单,并且可能比尝试找到内置方法更快。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文