我无法从包含许多NSArray
的{{1}}创建索引,我只想根据字典中的NSDictionaries
键来索引值。例如,每个字典看起来像这样:
username
目前我已简化了问题,因此我正在为{
username => "daspianist", //<- Only want to use this value to create index
objectId => "hjd72h3jd",
createdAt => "30-1-2014",
updatedAt => "30-1-2014"
}
NSArray
编制索引,我的工作如下:
NSStrings
我正在努力将我为NSStrings所做的事情转换为我的NSDictionaries中//Note that `stringArray` is passed to this method
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (char firstChar = 'a'; firstChar <= 'z'; firstChar++)
{
NSString *firstCharacter = [NSString stringWithFormat:@"%c", firstChar];
NSArray *content = [stringArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[cd] %@", firstCharacter]];
NSMutableArray *mutableContent = [NSMutableArray arrayWithArray:content];
if ([mutableContent count] > 0)
{
NSString *key = [firstCharacter uppercaseString];
[dict setObject:mutableContent forKey:key];
NSLog(@"%@: %u", key, [mutableContent count]);
}
}
键的值,并且不胜感激。谢谢!
更新
为了澄清,我感兴趣的结果词典看起来像这样
username
更新2
根据{
"a" => {
username => "aardvark",
otherKeys => "otherValues"
},
{
username => "applepicking",
otherKeys => "otherValues"
}
"d" => {
username => "daspianist",
otherKeys => "otherValues"
}
....
}
的答案输入解决方案,以方便使用:
Wain
答案 0 :(得分:1)
尝试实现以下功能:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray *charactersForSort = [[NSMutableArray alloc] init];
for (NSDictionary *item in array)
{
NSString *username = [item objectForKey:@"username"]
if (![charactersForSort containsObject:[username substringToIndex:1]])
{
[charactersForSort addObject:[username substringToIndex:1]];
}
}
return charactersForSort;
}
此处数组是一个 NSArray 的词典,您已在问题中提及。
答案 1 :(得分:1)
因此,content
是与当前密钥匹配的username
数组。目标是找到该数组中包含username
的所有词典。这是谓词的工作:
NSPredicate *p = [NSPredicate predicateWithFormat:@"username IN %@", content];
现在你可以这样做:
NSArray *values = [dictArray filteredArrayUsingPredicate:p];
[dict setObject:values forKey:key];