Search code examples
iosobjective-cnsmutablearraynsmutabledictionary

Objective-C: 'unrecognized selector sent to instance'


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
}

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.