将NSArray元素分组为NSMutableDictionary

时间:2013-06-18 11:19:03

标签: ios objective-c nsmutablearray

我有一个NSArray类似于以下格式的东西。

组数组是:

(
    "Q-1-A1",
    "Q-1-A9",
    "Q-2-A1",
    "Q-2-A5",
    "Q-3-A1",
    "Q-3-A8",
    "Q-4-A1",
    "Q-4-A4",
    "Q-10-A2",
    "Q-8-A2",
    "Q-9-A2",
    "Q-7-A1",
    "Q-5-A2"
)

现在我要做的就是将数组元素分组这样的东西。

1 = ( "Q-1-A1","Q-1-A9")
2 = ("Q-2-A1","Q-2-A5",) ...
10 =("Q-10-A2")

任何人都可以帮助我如何实现这一目标。

提前致谢。

4 个答案:

答案 0 :(得分:6)

尝试

NSArray *array = @[@"Q-1-A1",
                   @"Q-1-A9",
                   @"Q-2-A1",
                   @"Q-2-A5",
                   @"Q-3-A1",
                   @"Q-3-A8",
                   @"Q-4-A1",
                   @"Q-4-A4",
                   @"Q-10-A2",
                   @"Q-8-A2",
                   @"Q-9-A2",
                   @"Q-7-A1",
                   @"Q-5-A2"];

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

for (NSString *string in array) {

    NSArray *components = [string componentsSeparatedByString:@"-"];

    NSString *key = components[1];
    NSMutableArray *tempArray = dictionary[key];
    if (!tempArray) {
        tempArray = [NSMutableArray array];
    }
    [tempArray addObject:string];
    dictionary[key] = tempArray;
}

答案 1 :(得分:2)

创建一个NSMutableDictionary,然后遍历你的'group array'。

对于每个NSString对象:

  1. 获取组件的NSArraySeparatedByString:@“ - ”

  2. 使用第二个组件创建一个键,并从可变字典中检索该键的对象。如果它为nil,则将其设置为空的NSMutableArray。

  3. 将原始NSString添加到可变数组中。

答案 2 :(得分:1)

试试这个

NSArray *arrData =[[NSArray alloc]initWithObjects:@"Q-1-A1",@"Q-1-A9",@"Q-2-A1",@"Q-2-A5",@"Q-3-A1",@"Q-3-A8",@"Q-4-A1",@"Q-4-A4",@"Q-10-A2",@"Q-8-A2",@"Q-9-A2",@"Q-7-A1",@"Q-5-A2", nil ];
NSMutableDictionary *dictList = [[NSMutableDictionary alloc]init];

for (int i=0; i<[arrData count];i++) {
    NSArray *arrItem = [[arrData objectAtIndex:i] componentsSeparatedByString:@"-"];
    NSMutableArray *arrSplitedItems = [dictList valueForKey:[arrItem objectAtIndex:1]];
    if (!arrSplitedItems) {
        arrSplitedItems = [NSMutableArray array];
    }
    [arrSplitedItems addObject:[arrData objectAtIndex:i]];
    [dictList setValue:arrSplitedItems forKey:[arrItem objectAtIndex:1]];
}
NSArray *sortedKeys =[dictList allKeys];
NSArray *sortedArray = [sortedKeys sortedArrayUsingComparator:^(id str1, id str2) {
    return [((NSString *)str1) compare:((NSString *)str2) options:NSNumericSearch];
}];
for (int i=0; i<[sortedArray count]; i++) {
    NSLog(@"%@",[dictList objectForKey:[sortedArray objectAtIndex:i]]);
}

答案 3 :(得分:0)

listOfYourMainArray/// Its YOur main Array;
temArray = (NSArray *)listOfYourMainArray; // Add Your main array to `temArray`.
NSMutableDictionary *lastDic = [[NSMutableDictionary alloc] init]; /// YOu need to creat Dictionary for arrange your values.

for (int i = 0; i< listOfYourMainArray.count; i++)
{
   for (int j = 0 ; j < temArray.count; j ++)
   {
     if (([[temArray objectAtIndex:j] rangeOfString:[NSString stringWithFormat:@"Q-%d", i] options:NSCaseInsensitiveSearch].location != NSNotFound))
     {
         [lastDic setValue:[temArray objectAtIndex:j] forKey:[NSString stringWithFormat:@"%d", i]];
     }
   }  
}

NSLog(@"%@", lastDic)