Apple's String Format Specifiers document claims,
The format specifiers supported by the NSString formatting methods and CFString formatting functions follow the IEEE printf specification; … You can also use these format specifiers with the NSLog function.
But, while the printf
specification defines %C
as an equivalent for %lc
and %S
as an equivalent for %ls
, only %C
and %S
appear to work correctly with NSLog
and +[NSString stringWithFormat:]
.
For example, consider the following code:
#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;
}
Given the printf
specification, I would expect each of the above pairs to print the same thing. But, when I run the code, I get the following output:
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:
Am I doing something wrong, or is this a bug in Apple's code?