Question

I want to set the value of an object for a certain key, but I get this strange error. I also tried to use setValue instead of setObject and the routenArray2 also got the key in it.

NSMutableArray *routenArray = [[NSMutableArray alloc] init];
NSMutableArray *routenArray2 = [[NSMutableArray alloc] init];

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
abfahrt,@"Abfahrt", ankunft, @"Ankunft", dauer, @"Dauer", route, @"Route", kennung, @"Kennung", nil];

[routenArray addObject:dict];

for (int j = 0; j < routenArray.count; j++) {

    NSNumber *lNumber = [indexArray objectAtIndex:j];
    int l = [lNumber intValue];

    if (l == i) {
          [routenArray2 addObject:[[routenArray objectAtIndex:j] copy]];
    }
}

NSString *kennungString = [NSString stringWithFormat:@"%d", k];

for (int i = 0; i < routenArray2.count; i++) {
      [[routenArray2 objectAtIndex:i] setObject:kennungString forKey:@"Kennung"]; //this line is the problem
}
Was it helpful?

Solution

Use

[routenArray2 addObject:[[routenArray objectAtIndex:j] mutableCopy]];

Instead of

[routenArray2 addObject:[[routenArray objectAtIndex:j] copy]];

copy returns NSDictionary, you need mutableCopy to return NSMutableDictionary.

OTHER TIPS

Try this:

NSMutableDictionary *newDict = (NSMutableDictionary *)[routenArray2 objectAtIndex:i];
[newDict setObject:kennungString forKey:@"Kennung"];

Try typecasting in assign the value

[(NSMutableDictionary *)[routenArray2 objectAtIndex:i] setObject:kennungString forKey:@"Kennung"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top