iOS special characters, strange issue

我的梦境 提交于 2020-01-16 18:18:13

问题


I've got a very strange issue. I started an iOS App about three years ago (iOS-SDK 3.0), and since then went through the SDKs 4.0 and 5.0. Since 5.0 (or maybe 5.1) I suddenly started having problems with German special chars (ä ö ü ß).

Now I can't even initialize an NSString with special chars, this line:

NSString *str = @"abcäxyz";

gives the following warning:

Input conversion stopped due to an input byte that does not belong to the input codeset UTF-8

And this one:

NSLog(@"%@", strTemp);

gives:

abc

So it's stopping at the first special char. In other projects everything is fine. I can work with special chars without any problems.

Is it a configuration problem?

EDIT: Obviously it is a problem with the file encoding.

file -I myFile


is giving:

text/x-c++; charset=unknown-8bit

Trying to convert it with iconv gives me:

conversion from unknown-8bit unsupported


回答1:


What happens when you use the UTF-8 codes to initialize the string? Like so:

 NSString *s = [NSString stringWithFormat:@"%C", 0xc39f]; // should be ß 

As far as I know you should also be able to do this, but haven't tested it:

  NSString *s = [NSString stringWithUTF8String:"0xc39f"];

Try those and see what happens. There's a number of sites around that keep UTF-8 code tables for special characters, e.g. this one.




回答2:


As long as your file is encoded UTF-8, @"abcäxyz" should be fine, but the explicit form of embedding a literal unicode characters is \u????.

- (void)testGermanChar
{
    NSString *expected = @"abc\u00E4xyz";
    NSString *actual = @"abcäxyz";
    STAssertEqualObjects(expected, actual, @"the two strings should be equivalent");
}



回答3:


SOLVED: Changed the file encoding in Xcode: Click on the file you want to change the encoding of, then open the right panel (whats the name of this pane actually? any idea?) to edit the properties. There you see "Text Encoding" under "Text Settings". That is all.



来源:https://stackoverflow.com/questions/12315672/ios-special-characters-strange-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!