CFStringCreateWithFormat 期望什么样的字符串作为参数?
下面的示例应该适用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想通过
CFStringCreateWithFormat
包含CFStringRef
,请使用%@
。请参阅 Core Foundation 字符串编程指南的格式说明符部分。
%@
用于 Objective C 对象,或CFTypeRef
对象(CFStringRef
与CFTypeRef
兼容)% s
用于以 null 结尾的 8 位无符号字符数组(即普通 C 字符串)。%S
用于以 null 结尾的 16 位 Unicode 字符数组。CFStringRef 对象与“以 null 结尾的 16 位 Unicode 字符数组”不同。
Use
%@
if you want to include aCFStringRef
viaCFStringCreateWithFormat
.See the Format Specifiers section of Strings Programming Guide for Core Foundation.
%@
is for Objective C objects, ORCFTypeRef
objects (CFStringRef
is compatible withCFTypeRef
)%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”.
作为对其他答案中评论的回答,我建议发布者
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
char*
CFString
usingCFStringCreateWithCString
withkCFStringEncodingUTF8
as the encoding.Please, please do not use
%s
inCFStringCreateWithFormat
. 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.