【发布时间】:2009-03-20 21:02:46
【问题描述】:
Apple 的 String Format Specifiers 文件声称,
NSString格式化方法和CFString格式化函数支持的格式说明符遵循IEEE printf specification; …您还可以将这些格式说明符与 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;
}
鉴于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:
是我做错了什么,还是 Apple 代码中的错误?
【问题讨论】:
标签: cocoa unicode formatting printf