1

我之前发布了这个问题,以从用户首选项设置中获取最新的语言环境标识符。这是我得到的答案。

 #include <Foundation/Foundation.h>

    std::string get_user_locale()
    {
        NSLocale* locale = [NSLocale autoupdatingCurrentLocale];
        return std::string([[locale localeIdentifier] UTF8String]);
    }

int main()
{
    std::cout << "Locale is " << get_user_locale() << std::endl;
    return 0;
}

我创建了一个独立的 .mm 文件并尝试打印语言环境标识符字符串,但应用程序失败并出现以下堆栈

2019-11-06 05:40:12.879 App[9062:303135] +[NSLocale autoupdatingCurrentLocale]: unrecognized selector sent to class 0x7fff8f86c458
2019-11-06 05:40:12.894 App[9062:303135] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSLocale autoupdatingCurrentLocale]: unrecognized selector sent to class 0x7fff8f86c458'
*** First throw call stack:
(
        0   CoreFoundation                      0x00007fff36daeecd __exceptionPreprocess + 256
        1   libobjc.A.dylib                     0x00007fff62e66720 objc_exception_throw + 48
        2   CoreFoundation                      0x00007fff36e2c1c5 __CFExceptionProem + 0
        3   CoreFoundation                      0x00007fff36d50b40 ___forwarding___ + 1486
        4   CoreFoundation                      0x00007fff36d504e8 _CF_forwarding_prep_0 + 120
        5   testfun                             0x000000010e72096a main + 58
        6   libdyld.dylib                       0x00007fff63f34ed9 start + 1
        7   ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Locale is 

Abort (core dumped)

我在这里错过了什么吗?

4

0 回答 0