ios - 根据 NSDictionaries 的 NSArray 内的值创建 UITableView 的索引

标签 ios objective-c uitableview nsarray nsdictionary

我在从包含许多 NSDictionariesNSArray 创建索引时遇到问题,我想仅根据用户名对值进行索引字典中的键。如图所示,每个词典看起来都像这样:

{
     username => "daspianist", //<- Only want to use this value to create index
     objectId => "hjd72h3jd",
     createdAt => "30-1-2014",
     updatedAt => "30-1-2014"
}

目前,我已经简化了问题,以便为 NSStringsNSArray 建立索引,我所做的如下:

//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]);
        }
    }

我正在努力将我为 NSStrings 所做的事情转换为 NSDictionaries 中 username 键的值,并且希望得到任何指导。谢谢!

更新

为了澄清,我感兴趣的结果字典看起来像这样

{
    "a" => {
             username => "aardvark",
             otherKeys => "otherValues"
           },
           {
             username => "applepicking",
             otherKeys => "otherValues"
           }
    "d" => {
             username => "daspianist",
             otherKeys => "otherValues"
           }
      ....
}

更新2

为了便于使用,根据 Wain 的答案键入的解决方案:

 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    for (char firstChar = 'a'; firstChar <= 'z'; firstChar++)
    {
        NSString *firstCharacter = [NSString stringWithFormat:@"%c", firstChar];
        NSArray *content = [userNamesArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[cd] %@", firstCharacter]];
        NSMutableArray *mutableContent = [NSMutableArray arrayWithArray:content];
        NSPredicate *p = [NSPredicate predicateWithFormat:@"username IN %@", content];

        if ([mutableContent count] > 0)
        {
            NSString *key = [firstCharacter uppercaseString];
            NSArray *values = [originalDictionary filteredArrayUsingPredicate:p];
            [dict setObject:values forKey:key];
            NSLog(@"%@: %lu", key, (unsigned long)[mutableContent count]);
        }
    }

    NSLog(@"The dictionary is %@", dict);

最佳答案

因此,content 是与当前键匹配的username 数组。目标是找到该数组中包含 username 的所有字典。这是谓词的工作:

NSPredicate *p = [NSPredicate predicateWithFormat:@"username IN %@", content];

现在你可以做:

NSArray *values = [dictArray filteredArrayUsingPredicate:p];
[dict setObject:values forKey:key];

关于ios - 根据 NSDictionaries 的 NSArray 内的值创建 UITableView 的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21460812/

相关文章:

objective-c - 尽量减少重复代码?

objective-c - 使用 NSURLRequest 请求

ios - 适合 UITableViewCell 中的 textLabel

ios - 如何在后台线程 iOS 中运行一个进程

ios - 如何从自定义 View 中访问 View Controller 中的变量?

ios - 如何在将 NSData 转换为 NSString 后将 NSString 转换回 NSData?

objective-c - 我们如何将 double 或 float 重新解释为 NSUInteger 来创建哈希?

ios - 如何根据collectionView中的条件隐藏textview及其高度?

ios - 我的 TableViewCell 布局和滚动出现问题

ios - tableView 显示重复值