我有两个数组,一个名为'arrayGO'(存储来自数据库的用户列表),另一个名为'idtGO'(存储用户id列表)。arrayGO满足以下结构:
@"安德鲁"@"Aline-A"@"Bruno-B"@"Bola-B"@"Caio-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;
}
这个命令返回我想要的数组,在这个例子中是:
然而,我需要对存储用户'id '的变量做同样的事情,在这个cade中,数组'idtGO'满足以下结构:A = (艾琳,安德烈);B = (流星锤,布鲁诺);C = (Caio
);
@"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;
}
如果有人能帮我解决这个难题,我将非常感激。
好吧,伙计们,我试了又试,终于得到了这个谜题的答案,下面的代码显示了如何:
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);