2013-06-18 53 views
0

我有一個NSArray有點像下面的格式。將NSArray元素分組到NSMutableDictionary

組數組是:

(
    "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") 

任何人都可以請幫助我如何實現這一目標。

在此先感謝。

回答

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; 
} 
2

創建一個NSMutableDictionary,然後遍歷你的「組數組」。

對於每一個的NSString對象:

  1. 得到componentsSeparatedByString的NSArray的:@「 - 」

  2. 使用第二個組件來創建密鑰並從您的可變字典檢索該鍵的對象。如果它的nil然後將其設置爲空的NSMutableArray。

  3. 將原始NSString添加到可變數組中。

+2

這是巧合,我的答案看起來像一個字一個字對你所提到的情況。我永遠不會寫下步驟而不是寫代碼。 +1。 – Anupdas

+0

正在我的iPhone上書寫而無法訪問文本編輯器,另外我更願意解釋「如何」,以便OP可以學習某些內容,而不僅僅是剪切和粘貼... – ikuramedia

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