Eva*_*ase 5 unicode formatting cocoa printf
Apple的字符串格式说明符记录了声明,
NSString格式化方法和CFString格式化函数支持的格式说明符遵循IEEE printf规范 ; ...您也可以将这些格式说明符与NSLog函数一起使用.
但是,尽管printf规范定义%C作为等同于%lc和%S作为等同于%ls只%C和%S看起来与正常工作NSLog和+[NSString stringWithFormat:].
例如,请考虑以下代码:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
unichar str[3];
str[0] = 63743;
str[1] = 33;
str[2] = (unichar)NULL;
NSLog(@"NSLog");
NSLog(@"%%S: %S", str);
NSLog(@"%%ls: %ls", str);
NSLog(@"%%C: %C", str[0]);
NSLog(@"%%lc: %lc", str[0]);
NSLog(@"\n");
NSLog(@"+[NSString stringWithFormat:]");
NSLog(@"%%S: %@", [NSString stringWithFormat:@"%S", str]);
NSLog(@"%%ls: %@", [NSString stringWithFormat:@"%ls", str]);
NSLog(@"%%C: %@", [NSString stringWithFormat:@"%C", str[0]]);
NSLog(@"%%lc: %@", [NSString stringWithFormat:@"%lc", str[0]]);
[pool drain];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
鉴于printf规范,我希望上面的每一对都打印相同的东西.但是,当我运行代码时,我得到以下输出:
2009-03-20 17:00:13.363 UnicharFormatSpecifierTest[48127:10b] NSLog
2009-03-20 17:00:13.365 UnicharFormatSpecifierTest[48127:10b] %S: ?!
2009-03-20 17:00:13.366 UnicharFormatSpecifierTest[48127:10b] %ls: ?¯!
2009-03-20 17:00:13.366 UnicharFormatSpecifierTest[48127:10b] %C: ?
2009-03-20 17:00:13.367 UnicharFormatSpecifierTest[48127:10b] %lc:
2009-03-20 17:00:13.367 UnicharFormatSpecifierTest[48127:10b]
2009-03-20 17:00:13.368 UnicharFormatSpecifierTest[48127:10b] +[NSString stringWithFormat:]
2009-03-20 17:00:13.368 UnicharFormatSpecifierTest[48127:10b] %S: ?!
2009-03-20 17:00:13.369 UnicharFormatSpecifierTest[48127:10b] %ls: ?¯!
2009-03-20 17:00:13.369 UnicharFormatSpecifierTest[48127:10b] %C: ?
2009-03-20 17:00:13.370 UnicharFormatSpecifierTest[48127:10b] %lc:
Run Code Online (Sandbox Code Playgroud)
我做错了什么,或者这是Apple代码中的错误?
在Mac OS X上,<machine/_types.h>定义wchar_t为int,因此它是所有当前支持的体系结构上的四个字节(32位).
正如您所注意到的,printf(3)联机帮助页定义%S为等效于%ls,它采用指向某些wchar_t字符(wchar_t *)的指针.
但是,您链接到的Cocoa文档(及其CF等价物)确实%S单独定义:
%S:以空值终止的16位 Unicode字符数组
强调补充说.同样,也是如此%C.
所以,这不是一个错误.CF和Cocoa解释%S和%C不同于printf它和堂兄弟如何解释它们.CF和Cocoa将字符视为UTF-16,而printf(可能)将它们视为UTF-32.
在使用Core Services时,CF/Cocoa解释更有用,因为某些API(例如文件管理器)会将文本作为UniChars 数组传递,而不是CFString; 只要您终止该数组,就可以使用它%S来打印字符串.
| 归档时间: |
|
| 查看次数: |
7848 次 |
| 最近记录: |