0
std::string title = "中文";
NSString* str = [[NSString alloc] initWithFormat: @"test 中文 %s", title.c_str()]; // test 中文 ‰∏≠Êñá

I will get messy code. Can't I use %s to insert chinese in iOS ? How can I get correct result.

The real scene is the nativeLog.

void nativeLog2(std::string format, va_list args)
{
    NSString* str = [[NSString alloc] initWithFormat: [[NSString alloc] initWithUTF8String:format.c_str()] arguments:args];
    NSLog(str);
}

void nativeLog(std::string format, ...)
{
    va_list args;
    va_start(args, format);
    nativeLog2(format, args);
    va_end(args);
}

std::string title = "中文";
nativeLog("test 中文 %s", title.c_str()); // test 中文 ‰∏≠Êñá
Albert
  • 3
  • 2
  • The problem is not in `NSString`. This might help: https://stackoverflow.com/questions/50403342/how-do-i-properly-use-stdstring-on-utf-8-in-c – lazarevzubov Nov 30 '22 at 08:06
  • Make `title` an NSString. – matt Dec 01 '22 at 03:12
  • Actually the `title` is in `va_list`, I just use `NSString initWithFormat arguments` to init a NSString, but only get messy code. – Albert Dec 01 '22 at 04:00

0 Answers0