我有一本字典plist。该字典有A-Z数组,其中包含名称。我想按字母顺序排列每个数组中的名称,并将该顺序保存回plist。是枚举键并在每个数组上进行排序的最佳方法吗?
答案 0 :(得分:1)
// Full dictionary from plist
NSString *path = [[NSBundle mainBundle] pathForResource:@"Names" ofType:@"plist"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
self.mainDict = dict;
[dict release];
// keysArray is full of A-Z
NSMutableArray *keyArray = [[NSMutableArray alloc] init];
NSArray *sortedArray = [[mainDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
[keyArray addObjectsFromArray:sortedArray];
self.keysArray = keyArray;
[keyArray release];
// Sorts each array of names
int i = 0;
for (id keys in keysArray) {
NSArray *unsortedArray = [mainDict valueForKey:[keyArray objectAtIndex:i]];
NSArray *sorted = [unsortedArray sortedArrayUsingSelector:@selector(compare:)];
[mainDict setValue:sorted forKey:[keyArray objectAtIndex:i]];
i++;
}
看起来就是这样。 mainDict是一个NSMutableDictionary。 keysArray是一个NSArray。 现在问题是保存它。