2012-03-25 2 views
0

Ich habe ein sehr seltsames Verhalten, wenn ich versuche, ein NSMutableDictonary in eine NSMutableArrey einfügen, während die App in einer for-Schleife ist.iOS: Seltsames Verhalten beim Einfügen NSDictorary in NSArray während in for-Schleife (doppelte Einträge)

Das NSMutableDict wird für jeden Schritt erstellt und einem Array hinzugefügt. aber es funktioniert nicht ... wenn ich das Array nach der for-Schleife ausdrücke, ist jedes NSMableDictonary im Array gleich - Nach einiger Protokollierung habe ich gesehen, dass jeder for-step alle Dictionarys im Array ersetzt und eins hinzugefügt wird am ende ... das ist ein merkwürdiges verhalten und ich weiß nicht, was das verursacht ... Wenn ich die currentID (siehe code) zu dem Array insted des Diktons, am Ende, alles sieht gut aus ... whats Das Problem hier?

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

Sie verwenden immer die gleiche Instanz von tempDict. Verschieben Sie dieses alloc-init-Paar Ihres temporären Diktats in die Schleife. – Till

+0

oh, verdammt - danke;) – jacksbox

+0

Für schamlosen Ruf, ich füge das als eine Antwort hinzu - na ja und um unbeantwortete Fragen sicher auch zu verhindern. – Till

Antwort

1

Sie verwenden immer dieselbe Instanz von tempDict. Verschieben Sie dieses alloc-init-Paar Ihres temporären Diktats in die Schleife.

NSArray *relativeIAbnormality = [[NSArray alloc] init]; 
NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 
for (int q = 0; q < [measureData.list count]; q++) 
{ 
    NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init]; 
    ... 
}