2012-03-25 1 views
0

Я столкнулся с очень странным поведением, когда я пытаюсь вставить NSMutableDictonary в NSMutableArrey, в то время как приложение находится в цикле for.iOS: Странное поведение при вставке NSDictonary в NSArray во время цикла for (double entries)

NSMutableDict построен каждый шаг за шагом и добавлен в массив. но он не работает ... когда я распечатываю массив после цикла for, каждый NSMutableDictonary в массиве одинаков - после некоторого ведения журнала я видел, что каждый шаг за шагом, все словари в массиве заменяются и один добавляется в конце ... это странное поведение, и я не знаю, что вызывает это ... Если я добавлю currentID (см. код) в массив insted из-под диктатора, в конце концов, все выглядит нормально ... whats проблема здесь?

NSArray *relativeIAbnormality = [[NSArray alloc] init]; 
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init]; 
NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 

for (int q = 0; q < [measureData.list count]; q++) { 
    [tempDict removeAllObjects]; 
    NSString *currentId = [[measureData.list objectAtIndex:q] valueForKey:@"id"]; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == %@", currentId]; 
    NSInteger count = [[lastMeasureData.list filteredArrayUsingPredicate:predicate] count]; 

    if(count > 0){ 

     // get the answer Value for the CURRENT measure 
     float theValue = 0; 
     theValue = [[[[measureData.list objectAtIndex:q] objectForKey:@"propertys"] valueForKey:@"answerValue"] floatValue]; 

     theValue = theValue/100; 

     if(theValue > 10){ 
      theValue = 10; 
     }else if (theValue < 0) { 
      theValue = 0; 
     } 

     // get the answer Value for the LAST measure 
     float theNewValue = 0; 

     theNewValue = [[[[[lastMeasureData.list filteredArrayUsingPredicate:predicate] objectAtIndex:0] objectForKey:@"propertys"] valueForKey:@"answerValue"] floatValue]; 

     theNewValue = theNewValue/100; 

     if(theNewValue > 10){ 
      theNewValue = 10; 
     }else if (theNewValue < 0) { 
      theNewValue = 0; 
     } 

     // gets the reltaive 
     theValue = theValue - theNewValue; 
     NSNumber *dif = [NSNumber numberWithFloat:theValue]; 

     [tempDict setObject:currentId forKey:@"id"]; 
     [tempDict setObject:dif forKey:@"dif"]; 

     //NSLog(@"tempDict: %@", tempDict); 

     [tempArray addObject:tempDict]; 
     //NSLog(@"tempArray: %@", tempArray); 
    } 
} 
//NSLog(@"full tempArray: %@", tempArray); 
+3

Вы продолжаете использовать тот же самый экземпляр tempDict. Перенесите эту пару с параметром-выдержкой вашего temp-dict внутри цикла. – Till

+0

О, черт возьми, спасибо;) – jacksbox

+0

Для бесстыдной реплики, я добавлю это как ответ - ну, а также, конечно, для предотвращения неотвеченных вопросов. – Till

ответ

1

Вы продолжаете использовать тот же самый экземпляр tempDict. Перенесите эту пару с параметром-выдержкой вашего temp-dict внутри цикла.

NSArray *relativeIAbnormality = [[NSArray alloc] init]; 
NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 
for (int q = 0; q < [measureData.list count]; q++) 
{ 
    NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init]; 
    ... 
} 
Смежные вопросы