NSDictionary order does not match allKeys order

╄→гoц情女王★ 提交于 2019-12-31 07:40:45

问题


I've created NSDictionary of sorted arrays by name organized by first letter (see results below). When I use the command allKeys for that same Dictionary, the order is not the same. I need the order the same because this NSDictionary is used in UITableview and should be alphabetical.

 - (NSDictionary*) dictionaryNames {

NSDictionary *dictionary;
NSMutableArray *objects = [[NSMutableArray alloc] init];

NSArray *letters = self.exhibitorFirstLetter;
NSArray *names = self.exhibitorName;

for (NSInteger i = 0; i < [self.exhibitorFirstLetter count]; i++)
{
    [objects addObject:[[NSMutableArray alloc] init]];
}

dictionary = [[NSDictionary alloc] initWithObjects: objects forKeys:letters];

for (NSString *name in names) { 
    NSString *firstLetter = [name substringToIndex:1];

    for (NSString *letter in letters) {  //z, b
        if ([firstLetter isEqualToString:letter]) {
            NSMutableArray *currentObjects = [dictionary objectForKey:letter];
            [currentObjects addObject:name];
        }
    }

}

    NSLog(@"%@", dictionary);
NSLog(@"%@", [dictionary allKeys]);
return dictionary;

}

 B =     (
    "Baker's Drilling",
    "Brown Drilling"
);
C =     (
    "Casper Drilling"
);
J =     (
    "J's, LLC"
);
N =     (
    "Nelson Cleaning",
    "North's Drilling"
);
T =     (
    "Tim's Trucks"
);
Z =     (
    "Zach's Main",
    "Zeb's Service",
    "Zen's"
);

}

J,
T,
B,
N,
Z,
C

)


回答1:


NSDictionary is not an ordered collection. There's no way to control how it orders things, and it may change completely depending on OS version, device type, and dictionary contents.




回答2:


I just put the NSDictionary in sorted array:

- (NSArray*)sortAllKeys:(NSArray*)passedArray{
    NSArray* performSortOnKeys = [passedArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    return performSortOnKeys;
}


来源:https://stackoverflow.com/questions/22123148/nsdictionary-order-does-not-match-allkeys-order

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