Here is my code.
std::wstring sourceString = L"test\n newline.";
NSString* transformedString = [NSString stringWithFormat:@"%S", sourceString.c_str()];
I intended two strings’ contents to be same but transformedString is equivalent to @”t”.
How can I fix this with minimal edit?
(I have to use wstring because of unicode issue.)
On Mac OS X/iOS,
wchar_tis 32 bits wide (i.e. it represents UTF-32 characters, not UTF-16 characters.)%Scorresponds to a null-terminatedunichararray, andunicharis 16 bits wide, not 32 bits wide, so the character't'appears toNSStringto have a trailing null character (or leading null character on Big Endian targets) causing the string to be truncated.To convert to an
NSString, try:Note that the above assumes that
wchar_tholds a UTF-32 value, which is true on Mac OS X and most (all?) *NIXes, but false on Windows (wherewchar_tis 16 bits, equivalent tounichar.)