setlocale()在iOS模拟器中不起作用? [英] setlocale() doesn't work in iOS simulator?

查看:99
本文介绍了setlocale()在iOS模拟器中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:奇怪的是,setlocale()仅在iOS Simulator上失败,因此我修改了问题标题.它可以在实际设备上正常工作.

Update: Strangely, setlocale() only fails on the iOS Simulator so I have amended the question title. It works fine on actual devices.

我正在使用iOS 6下的本机(C/C ++)代码,并且需要格式化任意wchar_t字符串.但是,当格式化包含Latin-1代码页之外的代码点的字符串时,swprintf失败(带有errno = EILSEQ的返回值-1).

I'm working with native (C/C++) code under iOS 6 and I need to format arbitrary wchar_t strings. However, when formatting strings containing codepoints outside the Latin-1 codepage, swprintf fails (return value -1 with errno=EILSEQ).

wchar_t buff[256];
swprintf(buff, 256, L"\u00A9 %ls", L"ascii"); // works
swprintf(buff, 256, L"\u03A0 %ls", L"ascii"); // will return -1

提出相关问题后此处,问题似乎在于语言环境设置不正确(我已验证该解决方案在Mac OS X下可以正常工作).但这在iOS 6下似乎无效:

After asking a related question here, the problem appears to be that the locale is not set correctly (I have verified that the solution works under Mac OS X). But it seems to have no effect under iOS 6:

#include <locale.h>

setlocale(LC_CTYPE,"");

按照说明此处,我已复制/将语言环境文件手动添加到我的项目中,并设置PATH_LOCALE环境变量,但问题仍然存在:

Following the instructions here , I have copied/added the locale files manually to my project, and set the PATH_LOCALE environment variable, but the problem persists:

NSString* resourcePath=[[NSBundle mainBundle] resourcePath];
setenv("PATH_LOCALE", [resourcePath UTF8String], 1);
setlocale(LC_CTYPE,"en_US.UTF-8");

有人知道我如何让setlocale()在iOS 6下工作(同时仍使该应用程序被Apple Store接受)吗?

Does anyone know how I can get setlocale() to work under iOS 6 (while still having the app accepted by the Apple Store)?

推荐答案

我无法获得Stephane的解决方案.但是,这两个超级简单的解决方案都对我有用:

I couldn't get stephane's solution to work. But both of these super-simple solutions worked for me:

  1. setlocale(LC_CTYPE, "UTF-8")
  2. newlocale(LC_CTYPE_MASK, "UTF-8")结合了带有语言环境参数的foo_l变体wchar_t类.
  1. setlocale(LC_CTYPE, "UTF-8")
  2. newlocale(LC_CTYPE_MASK, "UTF-8") combined with the foo_l variant wchar_t classes that take a locale argument.

尽管iOS可能未附带完整的语言环境套件(包括en_US.UTF-8),但它似乎附带了无语言的基本UTF-8.但它仅适用于LC_CTYPE;尝试LC_ALL将失败.您当然可以使用

While iOS may not ship with a full suite of locales (including en_US.UTF-8), it appears to ship with a basic language-free UTF-8 one. But it only works with LC_CTYPE; trying LC_ALL will fail. You can of course test with

char *result = setlocale(LC_TYPE, "UTF-8");
Assert(result && strcmp(result, "UTF-8") == 0);

这篇关于setlocale()在iOS模拟器中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆