如何提取特定数据对于NSDictionary中的某些键到组合的NSArray具有相同的值

时间:2013-06-21 01:51:59

标签: iphone ios nsarray nsdictionary

现在我有一个这样的字典,这只是一个例子,我得到了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;
    }
)

我想提取dict具有相同的firstLetter并将它们组合成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;
    }
)

怎么做?

5 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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 就是你想要的

答案 2 :(得分: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];

  }

然后speDict就像

 A:
 //all firstletter is A
 myDict
 myDict
 myDict
 B:
 //all firstletter is B
 myDict
 myDict
 .......

答案 3 :(得分: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拥有你想要的东西。

答案 4 :(得分:0)

首先,您提供的样本是一系列dicts(不是问题说明的dict)。现在,查询此数组的最简单方法是使用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
}

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