我有两个数组,一个名为'arrayGO'(存储来自数据库的用户列表),另一个名为'idtGO'(存储一个用户ID列表)。 arrayGO 符合以下结构:
@ “安德烈-A”,@ “A线-A”,@ “布鲁诺-B”,@ “博拉-B”,@ “卡欧-C”
我有一个命令可以过滤这个数组,分隔破折号之后的字母,这样做组织数组:
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
NSMutableDictionary *dictionaryIDT = [NSMutableDictionary dictionary];
for (NSString *string in arrayGO) {
NSArray *components = [string componentsSeparatedByString:@"-"];
NSString *key = components[1];
NSMutableArray *tempArray = dictionary[key];
if (!tempArray){
tempArray = [NSMutableArray array];
}
[tempArray addObject:components[0]];
dictionary[key] = tempArray;
}
这个命令以我想要的方式返回数组,在这种情况下是:
A =( 艾琳, 安德烈 ); B =( 博拉, 布鲁诺 ); C =( 卡欧 );
但是我需要对存储用户'ids'的变量做同样的事情,在这个cade中,数组'idtGO'符合以下结构:
@ “4”,@ “1”,@ “12”,@ “22”,@ “33”
我想要做的就是我对名称列表所做的一样,所以在这种情况下和id,我试图得到以下结果:
0 =(4,1); 1 =(12,22); 2 =(33);
我试试这个:
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
NSMutableDictionary *dictionaryIDT = [NSMutableDictionary dictionary];
int idtIndex = 0;
int idtKeys = 0;
for (NSString *string in arrayGO) {
NSArray *components = [string componentsSeparatedByString:@"-"];
NSString *key = components[1];
NSString *keyIdt = [NSString stringWithFormat:@"%d", idtIndex];
NSMutableArray *tempArray = dictionary[key];
NSMutableArray *tempArrayidt = dictionaryIDT[keyIdt];
if (!tempArray){
tempArray = [NSMutableArray array];
tempArrayidt = [NSMutableArray array];
[tempArrayidt addObject:keyIdt];
dictionaryIDT[keyIdt] = tempArrayidt;
idtKeys++;
idtIndex++;
}else{
idtIndex++;
}
[tempArray addObject:components[0]];
dictionary[key] = tempArray;
}
如果有人帮我解决这个难题,我将不胜感激。
答案 0 :(得分:0)
好伙计们我努力尝试并最终得到这个谜题的答案,下面的代码说明了如何:
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
NSMutableDictionary *dictionaryIDT = [NSMutableDictionary dictionary];
NSString *keyTemp = @"nulo";
for (NSString *string in arrayGO) {
NSArray *components = [string componentsSeparatedByString:@"-"];
NSString *key = components[1];
NSString *keyIdt = [NSString stringWithFormat:@"%@", idt[idtKeys]];
NSString *keyIdtIndex = [NSString stringWithFormat:@"%d",idtIndex];
NSMutableArray *tempArray = dictionary[key];
NSMutableArray *tempArrayidt = dictionaryIDT[keyIdtIndex];
if (!tempArray){
tempArray = [NSMutableArray array];
tempArrayidt = [NSMutableArray array];
}
[tempArray addObject:components[0]];
dictionary[key] = tempArray;
if([keyTemp isEqual: @"nulo"]){
//Veririca se a variavel keyTemp é igual a zero, se ela for isso quer dizer que é a primeira vez que estamos aqui, e se for a variavel keyTemp Sera igual a key
keyTemp = key;
[tempArrayidt addObject:keyIdt];
dictionaryIDT[keyIdtIndex] = tempArrayidt;
idtKeys++;
}else{
//Se caimos aqui entao quer dizer que jaé a segunda vez que nos estamos caindo aqui, neste caso devemos verificar se a keyTemp ainda é a mesma
if([key isEqual: keyTemp]){
//Se for igual é porque nada mudou, ainda estamos tratando aquela chave, entao...
[tempArrayidt addObject:keyIdt];
dictionaryIDT[keyIdtIndex] = tempArrayidt;
idtKeys++;
}else{
//Se nao é porque a key mudou, entao agora nos devemos aumentar o idtIndex, redefinir o idtKeys e falar que a key é igual a keyTemp
idtIndex++;
keyIdtIndex = [NSString stringWithFormat:@"%d",[keyIdtIndex intValue] + 1];
idtKeys++;
keyTemp = key;
//Agora faremos o mesmo esquema de sempre...
[tempArrayidt addObject:keyIdt];
dictionaryIDT[keyIdtIndex] = tempArrayidt;
}
}
}
NSLog(@"%@",dictionaryIDT);