0

现在我有一本这样的字典,这只是一个例子,我得到了 A 到 Z:

(
        {
        id = 13;
        name = "Roll";
        firstLetter = R;
    },
        {
        id = 14;
        name = "Scroll";
        firstLetter = S;
    },
        {
        id = 16;
        name = "Rock";
        firstLetter = R;
    },
        {
        id = 17;
        name = "Start";
        firstLetter = S;
    }
)

我想提取具有相同 firstLetter 的 dict 并将它们组合成一个 NSArray 对象。预期结果如下:

R 数组:

(
        {
        id = 13;
        name = "Roll";
        firstLetter = R;
    },
        {
        id = 16;
        name = "Rock";
        firstLetter = R;
    }
)

和 S 数组:

(

    {
        id = 14;
        name = "Scroll";
        firstLetter = S;
    },
        {
        id = 17;
        name = "Start";
        firstLetter = S;
    }
)

怎么做?

4

5 回答 5

2

我相信更好的方法是Saohooou建议的方法

但它可以优化为

NSArray *array = @[@{@"id": @13,@"name":@"Roll",@"firstLetter":@"R"},
                   @{@"id": @14,@"name":@"Scroll",@"firstLetter":@"S"},
                   @{@"id": @15,@"name":@"Rock",@"firstLetter":@"R"},
                   @{@"id": @16,@"name":@"Start",@"firstLetter":@"S"}];

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[array enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) {
    NSString *key = dict[@"firstLetter"];
    NSMutableArray *tempArray = dictionary[key];
    if (!tempArray) {
        tempArray = [NSMutableArray array];
    }
    [tempArray addObject:dict];
    dictionary[key] = tempArray;
}];
NSLog(@"%@",dictionary);
于 2013-06-21T05:10:31.593 回答
0

我假设您将 dict 组织为 NSArray。

NSMutableDictionary* result = [NSMutableDictionary dictionary]; // NSDictionary of NSArray
for (id entry in dict) {
    NSString* firstLetter = [entry firstLetter];

    // Find the group of firstLetter 
    NSMutableArray* group = result[firstLetter];
    if (group == nil) {
        // No such group --> create new a new one and add it to the result
        group = [NSMutableArray array];
        result[firstLetter] = group;
    }

    // Either group has existed, or has been just created
    // Add the entry to it
    [group addObject: entry];
}

result拥有你想要的东西。

于 2013-06-21T02:18:49.507 回答
0

尝试这个

 NSString *currentStr;
 //this int is to detect currentStr
 NSInteger i;

 NSMutableArray* R_Array = [[NSMutableArray alloc] init];
 NSMutableArray* S_Array = [[NSMutableArray alloc] init];


 for (NSDictionary *myDict in MyDictArray){

    NSString *tempStr = [myDict objectForKey:@"firstLetter"];
    if(currentStr = nil && [currentStr isEqualToString:""]){
       currentStr = tempStr;

       if([currentStr isEqualToString:"R"] ){
          [R_Array addObject:myDict];
          i = 0;
       }else{
          [S_Array addObject:myDict];
          i = 1;
       }
    }else{
       if([currentStr isEqualToString:tempStr]){
          (i=0)?[R_Array addObject:myDict]:[S_Array addObject:myDict];
       }else{
          (i=0)?[R_Array addObject:myDict]:[S_Array addObject:myDict];
       }
    }

 }

根据您的字典。只有两种类型,所以我只创建了两个数组并使用 if-else 来解决问题。如果有多个值,您可以尝试使用 switch-case 来执行此操作。

我们开工吧

  NSMutaleDictionary * speDict = [[NSMutableDictionary alloc] init];
  for(i=0;i<26;i++){
     switch (i){
            case 0:
                [speDict setObject:[NSMutableArray alloc] init] forKey:@"A"];
                break;
            case 1:
                [speDict setObject:[NSMutableArray alloc] init] forKey:@"B"];
                break;
            Case 2:
                [speDict setObject:[NSMutableArray alloc] init] forKey:@"C"];
                break;
            ...........
            Case 25:
                [speDict setObject:[NSMutableArray alloc] init] forKey:@"Z"];
                break;
        }
  }

  for (NSDictionary *myDict in MyDictArray){

     NSString *tempStr = [myDict objectForKey:@"firstLetter"];

     switch (tempStr)
            case A:
                [self addToMySpeDictArrayWithObject:myDict andStr:temStr];
                break;
            case B:
                [self addToMySpeDictArrayWithObject:myDict andStr:temStr];
                break;
            Case C:
                [self addToMySpeDictArrayWithObject:myDict andStr:temStr];
                break;
            ...........
            Case Z:
                [self addToMySpeDictArrayWithObject:myDict andStr:temStr];
                break;
  }

  -(void)addToMySpeDictArrayWithObject:(NSDictionary*)_dict andStr:(NString*)_str
  {
       NSMutableArray *tempArray = [speDict objectForKey:_str];
       [tempArray addObject:_dict];

  }

那么预测就像

 A:
 //all firstletter is A
 myDict
 myDict
 myDict
 B:
 //all firstletter is B
 myDict
 myDict
 .......
于 2013-06-21T02:06:07.063 回答
0
                    NSMutableDictionay *dic = [NSMutableDictionay dictionay];
                    for ( YourObject *obj in yourDic.allValues )
                    {

                        NSMutableArray *dateArray = dic[obj.firstLetter];
                        if ( !dateArray )
                        {
                            dateArray = [NSMutableArray array];

                            [dic setObject:dateArray forKey:obj.firstLetter];
                        }

                        [dateArray addObject:obj];
                    }

所以dic是你想要的

于 2013-06-21T01:59:42.847 回答
0

首先,您提供的示例是一个字典数组(不是问题注释中的字典)。现在,查询此数组的最简单方法是使用NSPredicate. 可能是这样的:

NSArray *objects = ...; // The array with dicts

NSString *letter = @"S"; // The letter we want to pull out
NSPredicate *p = [NSPredicate predicateWithFormat:@"firstLetter == %@", letter];

NSArray *s = [objects filteredArrayUsingPredicate:p]; // All the 'S' dicts

如果由于某种原因您需要对所有对象进行分组而不必每次都要求特定的字母,您可以尝试以下操作:

// Grab all available firstLetters
NSSet *letters = [NSSet setWithArray:[objects valueForKey:@"firstLetter"]];

for (NSString *letter in letters)
{
    NSPredicate *p = [NSPredicate predicateWithFormat:@"firstLetter == %@", letter];
    NSArray *x = [objects filteredArrayUsingPredicate:p];
    // Do something with 'x' 
    // For example append it on a mutable array, or set it as the object
    // for the key 'letter' on a mutable dict
}

当然,您可以通过实现一种基于字母过滤数组的方法来进一步优化这种方法。我希望这是有道理的。

于 2013-06-21T08:46:29.320 回答