NSString替换unicode字符

我正在使用服务器,我必须将文本下载到我的iOS应用程序。 唯一的问题:例如“éàç”等所有字符都被“\ U008”取代。 有没有办法解决这个问题,用正确的字符替换这个代码?

尝试使用以下方法解析收到的文本(textToParse变量):

NSString *encodedString = textToParse; NSString *decodedString = [NSString stringWithUTF8String:[encodedString cStringUsingEncoding:[NSString defaultCStringEncoding]]]; 

我测试了一些编码和NSMacOSRomanStringEncoding合适。

我的测试是:

 NSString *encodedString = [NSString stringWithCString:"Você realmente deseja sair da área restrita" encoding:NSMacOSRomanStringEncoding]; 

请记住,消息必须是C字符串( "string" )而不是NSString@"string"

您可以获取字符缓冲区并validation每个字符,如下所示:

 - (NSString *) removeUnicode:(NSString *) unicodeString { NSUInteger len = [unicodeString length]; unichar buffer[len+1]; [unicodeString getCharacters:buffer range:NSMakeRange(0, len)]; unichar okBuffer[len+1]; int index = 0; for(int i = 0; i < len; i++) { if(buffer[i] < 128) { okBuffer[index] = buffer[i]; index = index + 1; } } NSString *removedUnicode = [[NSString alloc] initWithCharacters:okBuffer length:index]; return removedUnicode; } 

或者您可以使用此示例:

 NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:[NSCharacterSet alphanumericCharacterSet]] invertedSet]; stringWithOutUnicode = [[stringWithUnicode componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""]; 

并且您可以创建自己的有效字符集并获取不允许的字符

 NSString *allowedCharacters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString: allowedCharacters] invertedSet];