Reputation: 1819
In my Native Library I used to have this function to return a NSString
out of a std::string
:
- (NSString*)return_my_cpp_string {
NSString* result = [NSString stringWithCString:my_cpp_string.c_str() encoding:[NSString defaultCStringEncoding]];
return result;
}
I now need to update this function so that it rather returns a NSString
out of an std::u16string
but I can't find any obvious way to do so.
Upvotes: 4
Views: 401
Reputation: 595402
Use stringWithCharacters:length
instead of stringWithCString:encoding
.
stringWithCharacters
takes a pointer to a C-style array of UTF-16 code units as input. std::u16string
is a UTF-16 encoded string, so you can use its data as-is, eg:
- (NSString*)return_my_cpp_string {
const unichar *pchars = (const unichar *) my_cpp_string.c_str();
NSString* result = [NSString stringWithCharacters:pchars length:my_cpp_string.size()];
return result;
}
Upvotes: 3