Question

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);
Was it helpful?

Solution

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 NSDictionary uses 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.

OTHER TIPS

Try doing something like this:

NSString *someObject = //your object that needs decode
        NSString *decodedObject = [NSString
            stringWithCString:[someObject cStringUsingEncoding:NSUTF8StringEncoding]
            encoding:NSNonLossyASCIIStringEncoding];
        NSLog(@"decodedObject = %@", decodedObject);

Edit: There's probably something wrong in your code someplace else because this:

NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    [tempArray addObject:@"בדיקה"];

    [[NSUserDefaults standardUserDefaults] setObject:tempArray forKey:@"test"];
    [[NSUserDefaults standardUserDefaults] synchronize];    
    NSMutableArray *test2=[[NSUserDefaults standardUserDefaults] objectForKey:@"test"];    
    NSLog(@"test2: %@",[test2 objectAtIndex:0]);

works fine.

Well, can you post the code behind sharedPrefs?

When i tried to save a simple hebrew to userdefault it worked just fine.

NSString *tempKeyToStore = @"KEY";

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

[tempArray addObject:@"שלום שלום"];

[[NSUserDefaults standardUserDefaults] setObject:tempArray forKey:tempKeyToStore];
[[NSUserDefaults standardUserDefaults] synchronize];

Read it later -

//
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSMutableArray *tempArray = [defaults objectForKey:@"KEY"];

for (NSString* str in tempArray)
{
    NSLog(@"%@", str);
}

Hope i helped..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top