Search code examples
objective-cnsstringobjective-c++foundationnsstringencoding

How to convert c++ std::u32string to NSString?


I am developing a bridge between C++ and swift. And I need to convert C++ std::u32string to NSString. Here the code I tried:

u32string str = U"some string";
NSLog(@"%@", [[NSString alloc] initWithBytes:str.data() length:str.size() * sizeof(char32_t) encoding:NSUTF32StringEncoding]);

However initWithBytes returns nil.


Solution

  • Don't ask me why, but it seems to work if:

    • I insert a byte order mark (BOM) at the start of the string:
      u32string str = U"\uFEFFsome string";
    • or I use NSUTF32LittleEndianStringEncoding instead of NSUTF32StringEncoding.

    Inserting byte order marks all over the place isn't terribly practical, so I guess you need to define your own constant which evaluates to either the little or big endian versions of the encoding constant depending on the platform being compiled.

    This appears to be a quirk in Foundation, nothing specific to Objective-C++ or your use of std::u32string.