MdaG
MdaG

Reputation: 2730

Unicode characters gets messed up when used as key in NSMutableDictionary

When I use the following keys Ä,Å,Ö in a NSMutableDictionary they get converted into "\U00c4", "\U00c5" and "\U00d6". Causing my software to miss the objects associated with the mentioned keys as I use the original form (Å, Ä and Ö) in the code. The language is Swedish and I want this to work for all countries, so the conversion isn't a desired behavior.

What am I doing wrong here?

How can I avoid the characters to be converted?

Code:

- (void)populateNavigationViewWithProjects:(id)projects
{
    [self.navigationViewItems removeAllObjects];
    BOOL keyExists = NO;
    NSString* firstLetterInProjectName = @"";
    for (id project in projects) {
        firstLetterInProjectName = [[[project valueForKey:@"name"] uppercaseString] substringToIndex:1];
        keyExists = [[self.navigationViewItems allKeys] containsObject:firstLetterInProjectName];
        if (keyExists) {
            [[self.navigationViewItems objectForKey:firstLetterInProjectName] addObject:project];
        }
        else {
            NSMutableArray* listOfProjects = [[NSMutableArray alloc] initWithObjects:project, nil];
            [self.navigationViewItems setValue:listOfProjects forKey:firstLetterInProjectName];
            [listOfProjects release];
        }
    }
    [self.tableView reloadData];
}

navigationViewItems is accessed in:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString* sectionTitle = [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
    BOOL sectionContainsData = [[self.navigationViewItems allKeys] containsObject:sectionTitle];
    if (sectionContainsData) {
        return sectionTitle;
    }
    return @"";
}

Objects listed under Å,Ä and Ö won't be listed due to this.

Upvotes: 0

Views: 451

Answers (1)

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

you could call -precomposedStringWithCanonicalMapping before using the string.

NSString function to deal with unicode content.

precomposedStringWithCanonicalMapping:
decomposedStringWithCanonicalMapping:

Upvotes: 1

Related Questions