NSDictionary order does not match allKeys order

2019-03-07 03:23发布

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

)

2条回答
可以哭但决不认输i
2楼-- · 2019-03-07 03:55

I just put the NSDictionary in sorted array:

- (NSArray*)sortAllKeys:(NSArray*)passedArray{
    NSArray* performSortOnKeys = [passedArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    return performSortOnKeys;
}
查看更多
你好瞎i
3楼-- · 2019-03-07 04:06

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.

查看更多
登录 后发表回答