Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 关于CFLocaleCopyCurrent API返回值不正确_Macos_Cocoa_Foundation_Core Foundation_Macos High Sierra - Fatal编程技术网

Macos 关于CFLocaleCopyCurrent API返回值不正确

Macos 关于CFLocaleCopyCurrent API返回值不正确,macos,cocoa,foundation,core-foundation,macos-high-sierra,Macos,Cocoa,Foundation,Core Foundation,Macos High Sierra,在我的工作场所,我们的情况如下: #include <CoreFoundation/CoreFoundation.h> #include <iostream> #include <string> #include <vector> #include <memory> #include <boost/cast.hpp> // Reference release struct refe

在我的工作场所,我们的情况如下:

#include <CoreFoundation/CoreFoundation.h>  
#include <iostream>  
#include <string>  
#include <vector>  
#include <memory>  
#include <boost/cast.hpp>  

   // Reference release  
    struct reference_close  
    {  
        void operator()(const void *ref) const  
        {  
            CFRelease(static_cast<CFTypeRef>(ref));  
        }  
    }; // end of reference_close structure  

    typedef std::unique_ptr<const void, reference_close>  reference_uptr;  

     std::string get_user_locale()  
    {  
        reference_uptr  ref_ptr(CFLocaleCopyCurrent());  
        CFLocaleRef     locale_ref(static_cast<CFLocaleRef>(ref_ptr.get()));  
        if (locale_ref == nullptr)  
        {  
            return std::string();  
        }  
        const size_t       default_size(128);  
        std::vector<char>  buff(default_size);  
        CFStringRef        str_ref(CFLocaleGetIdentifier(locale_ref));  
//        CFStringRef        str_ref((CFStringRef)CFLocaleGetValue(locale_ref,kCFLocaleLanguageCode));  
       if (str_ref != nullptr)  
        {  
            CFIndex  len(CFStringGetLength(str_ref) + 1);  
            if (len > boost::numeric_cast<CFIndex>(default_size))  
            {  
                buff.resize(len);  
            }  

            buff[0] = 0;  
            if (!CFStringGetCString(str_ref, &buff[0], len, kCFStringEncodingISOLatin1))  
            {  
                return std::string();  
            }  
        }  

        return std::string(&buff[0]);  
    } // end of get_user_locale()  

int main()  
{  
    std::cout << "get_user_locale() : "<<get_user_locale() << std::endl;  

    return 0;  
}  
#包括
#包括
#包括
#包括
#包括
#包括
//参考版本
结构引用\u关闭
{  
void运算符()(常量void*ref)常量
{  
碳纤维释放(静态_铸造(参考));
}  
}; // 参考端\闭合结构
typedef标准::唯一的ptr参考\u uptr;
std::string get\u user\u locale()
{  
reference_uptr ref_ptr(CFLocaleCopyCurrent());
CFLocaleRef locale_ref(static_cast(ref_ptr.get());
if(locale_ref==nullptr)
{  
返回std::string();
}  
常量大小\u t默认大小(128);
标准::矢量buff(默认_大小);
CFStringRef str_ref(CFLocaleGetIdentifier(locale_ref));
//CFStringRef str_ref((CFStringRef)CFLocaleGetValue(locale_ref,kCFLocaleLanguageCode));
如果(str_ref!=nullptr)
{  
CFIndex len(CFStringGetLength(str_ref)+1);
if(len>boost::numeric_cast(默认大小))
{  
buff.调整大小(len);
}  
buff[0]=0;
if(!CFStringGetCString(str_ref,&buff[0],len,kcfStringCodingIsolatin1))
{  
返回std::string();
}  
}  
返回std::string(&buff[0]);
}//get\u user\u locale()结束
int main()
{  

std::cout我们得到错误值的原因是我们的应用程序没有本地化到我们期望的区域设置中,例如
ru
。 我们通过在应用程序的
Contents/Resources
目录中添加一个空的
ru.lproj
目录来纠正这个问题,API开始给出正确的答案

#import <Foundation/Foundation.h>  
int main()  
{  
    @autoreleasepool  
    {   
        NSLog(@"localeIdentifier: %@", [[NSLocale currentLocale] localeIdentifier]);  
    }   
}