CFStringCreateWithFormat 期望什么样的字符串作为参数?

发布于 2024-08-17 08:38:19 字数 443 浏览 5 评论 0原文

下面的示例应该适用于 Unicode 字符串,但事实并非如此。


CFStringRef aString =  CFSTR("one"); // in real life this is an Unicode string
CFStringRef formatString = CFSTR("This is %s example"); // also tried %S but without success
CFStringRef resultString = CFStringCreateWithFormat(NULL, NULL, formatString, aString);

// Here I should have a valid sentence in resultString but the current result is like aString would contain garbage.

The below example should work with Unicode strings but it doesn't.


CFStringRef aString =  CFSTR("one"); // in real life this is an Unicode string
CFStringRef formatString = CFSTR("This is %s example"); // also tried %S but without success
CFStringRef resultString = CFStringCreateWithFormat(NULL, NULL, formatString, aString);

// Here I should have a valid sentence in resultString but the current result is like aString would contain garbage.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

在风中等你 2024-08-24 08:38:19

如果您想通过 CFStringCreateWithFormat 包含 CFStringRef,请使用 %@

请参阅 Core Foundation 字符串编程指南的格式说明符部分

  • %@ 用于 Objective C 对象,或 CFTypeRef 对象(CFStringRefCFTypeRef 兼容)
  • % s 用于以 null 结尾的 8 位无符号字符数组(即普通 C 字符串)。
  • %S 用于以 null 结尾的 16 位 Unicode 字符数组。

CFStringRef 对象与“以 null 结尾的 16 位 Unicode 字符数组”不同。

Use %@ if you want to include a CFStringRef via CFStringCreateWithFormat.

See the Format Specifiers section of Strings Programming Guide for Core Foundation.

  • %@ is for Objective C objects, OR CFTypeRef objects (CFStringRef is compatible with CFTypeRef)
  • %s is for a null-terminated array of 8-bit unsigned characters (i.e. normal C strings).
  • %S is for a null-terminated array of 16-bit Unicode characters.

A CFStringRef object is not the same as “a null-terminated array of 16-bit Unicode characters”.

徒留西风 2024-08-24 08:38:19

作为对其他答案中评论的回答,我建议发布者

  • 以可移植的方式将 UTF8 字符串生成为 char*
  • ,并在最后一刻将其转换为 CFString< /code> 使用 CFStringCreateWithCString 并以 kCFStringEncodingUTF8 作为编码。

请不要在 CFStringCreateWithFormat 中使用 %s。请不要依赖“系统编码”,在西欧环境中为 MacRoman,但在其他语言中则不然。系统编码的概念本质上是脑死亡的,尤其是在东亚环境(我来自那里),甚至ASCII代码范围内的字符(低于127!)都会被修改。如果你依赖“系统编码”,地狱就会崩溃。幸运的是,从 10.4 开始,所有使用“系统编码”的方法现在都已弃用,除了 %s...。

很抱歉我为这个小主题写了这么多,但几年前确实很遗憾,当时有许多不错的应用程序由于这种“系统编码”而无法在日本/韩国 Mac 上运行。请参考我几年前写的这个详细解释,如果你有兴趣的话。

As an answer to the comment in the other answer, I would recommend the poster to

  • generate a UTF8 string in a portable way into char*
  • and, at the last minute, convert it to CFString using CFStringCreateWithCString with kCFStringEncodingUTF8 as the encoding.

Please, please do not use %s in CFStringCreateWithFormat. Please do not rely on the "system encoding", which is MacRoman on Western European environments, but not in other languages. The concept of the system encoding is inherently brain-dead, especially in east Asian environments (which I came from) where even the characters inside ASCII code range (below 127!) is modified. Hell breaks loose if you rely on "system encoding". Fortunately, since 10.4, all of the methods which use "system encoding" are now deprecated, except %s... .

I'm sorry I write this much for this small topic, but it was a real pity a few years ago when there were many nice apps which didn't work on Japanese/Korean Macs because of just this "system encoding." Please refer to this detailed explanation which I wrote a few years ago, if you're interested.

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