本文介绍了将希伯来文保存到NSUserDefaults返回奇怪的编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我保存希伯来语文本到NAMutableArray,然后将其保存到NSUserDefalts,这样:

  numOfWallPosts ++; 
NSString * tempKeyToStore = [NSString stringWithFormat:@wall_post_%d,numOfWallPosts]; // wall_post_%d

NSMutableArray * tempArray = [[NSMutableArray alloc] init];

[tempArray addObject:currentOwner];
[tempArray addObject:currentTitle];
[tempArray addObject:currentText];
[tempArray addObject:currentDate];
[tempArray addObject:currentTime];
[tempArray addObject:currentImageURL];
[tempArray addObject:currentWallID];


[self.sharedPrefs setObject:tempArray forKey:tempKeyToStore];
[self.sharedPrefs synchronize];

当我想要收到希伯来文文本时,我会收到如下文本:

 墙消息:(
\U05de\U05e2\U05e8\U05db\U05ea,
\\ \\U05ea\U05e8\U05d2\U05d9\U05dc \U05e4\U05d9\U05e7\U05d5\U05d3 \U05d4\U05e2\U05d5\U05e8\U05e3,
\U05d0\U05d6\U05e2\U05e7\U05ea \U05ea\U05e8\U05d2\U05d5\U05dc \U05d1\U05e9\U05e2\U05d4 19:05,
27/05/13,
13:16,
http:// blabla ......,
9

我尝试格式化 UTF8String 中的文本,使用 NSUTF8StringEncoding ,我仍然以这种方式获取文本。



我可以正确保存文本还是编码回到正确的希伯来语?



编辑:
这是非常奇怪的,虽然文本保存st范围,我把它拉回来,我得到正确的文本。
但是当我将其保存到NSUserDefaults时,它会显示出这个奇怪的编码。

  NSString * tempKeyToStore = [NSString stringWithFormat :@wall_post_%d,indexPath.row]; 
NSMutableArray * tempArray = [[NSMutableArray alloc] init];
NSLog(@\\\
\\\
Wall messages:%@,[self.sharedPrefs objectForKey:tempKeyToStore]);

tempArray = [self.sharedPrefs objectForKey:tempKeyToStore];
NSString * tempOwner = [tempArray objectAtIndex:0];
NSLog(@\\\
\\\
OWNER:%@,tempOwner);


解决方案

如果您使用 NSLog @%@,...)打印数组字典的描述,所有非ASCII字符都使用 \Unnnn 转义序列:

  NSArray * a = [NSArray arrayWithObject :@ מערכת]; 
NSLog(@%@,a);

输出:

 

\U05de\U05e2\U05e8\U05db\U05ea

(原因是描述方法 NSArray NSDictionary
使用格式,
,如名称所示,仅允许ASCII字符。)



如果您打印字符串的描述,则所有字符都将正确显示:

  NSLog(@%@,[a objectAtIndex:0]); 

输出:

 
מערכת

所以这只是一个显示问题。


I'm saving Hebrew text to NAMutableArray, then I save it to NSUserDefalts, this way:

numOfWallPosts++;
        NSString *tempKeyToStore = [NSString stringWithFormat:@"wall_post_%d", numOfWallPosts]; // wall_post_%d

        NSMutableArray *tempArray = [[NSMutableArray alloc] init];

        [tempArray addObject:currentOwner];
        [tempArray addObject:currentTitle];
        [tempArray addObject:currentText];
        [tempArray addObject:currentDate];
        [tempArray addObject:currentTime];
        [tempArray addObject:currentImageURL];
        [tempArray addObject:currentWallID];


        [self.sharedPrefs setObject:tempArray forKey:tempKeyToStore];
        [self.sharedPrefs synchronize];

When I want to get the Hebrew text back I get text like this:

Wall messages: (
    "\U05de\U05e2\U05e8\U05db\U05ea",
    "\U05ea\U05e8\U05d2\U05d9\U05dc \U05e4\U05d9\U05e7\U05d5\U05d3 \U05d4\U05e2\U05d5\U05e8\U05e3",
    "\U05d0\U05d6\U05e2\U05e7\U05ea \U05ea\U05e8\U05d2\U05d5\U05dc \U05d1\U05e9\U05e2\U05d4 19:05",
    "27/05/13",
    "13:16",
    "http://blabla......",
    9
)

I tried to format the text in UTF8String, and with NSUTF8StringEncoding and I still get the text this way.

Can I save the text in proper way or encode it back to proper Hebrew?

EDIT:This is so strange, although the text saved strange, I pull it back and I get the text correctly.But when I save them to NSUserDefaults, it show me this strange encoding.

NSString *tempKeyToStore = [NSString stringWithFormat:@"wall_post_%d", indexPath.row];
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    NSLog(@"\n\nWall messages: %@", [self.sharedPrefs objectForKey:tempKeyToStore]);

    tempArray = [self.sharedPrefs objectForKey:tempKeyToStore];
    NSString *tempOwner = [tempArray objectAtIndex:0];
    NSLog(@"\n\nOWNER: %@", tempOwner);
解决方案

If you use NSLog(@"%@", ...) to print the description of an array or dictionary, all non-ASCII characters are printed using a \Unnnn escape sequence:

NSArray *a = [NSArray arrayWithObject:@"מערכת"];
NSLog(@"%@", a);

Output:

(
   "\U05de\U05e2\U05e8\U05db\U05ea"
)

(The reason is that the description method of NSArray and NSDictionaryuses the Old-Style ASCII Property Lists format,which, as the name indicates, allows only ASCII characters.)

If you print the description of a string, all characters are properly displayed:

NSLog(@"%@", [a objectAtIndex:0]);

Output:

מערכת

So this is only a display issue.

这篇关于将希伯来文保存到NSUserDefaults返回奇怪的编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 07:44