我想为某个键设置一个对象的值,但是我得到了这个奇怪的错误。 我也尝试使用setValue而不是setObject,而routenArray2也得到了密钥。
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
}
答案 0 :(得分:1)
使用
[routenArray2 addObject:[[routenArray objectAtIndex:j] mutableCopy]];
而不是
[routenArray2 addObject:[[routenArray objectAtIndex:j] copy]];
copy
返回NSDictionary
,您需要mutableCopy
才能返回NSMutableDictionary
。
答案 1 :(得分:0)
试试这个:
NSMutableDictionary *newDict = (NSMutableDictionary *)[routenArray2 objectAtIndex:i];
[newDict setObject:kennungString forKey:@"Kennung"];
答案 2 :(得分:0)
在分配值
时尝试进行类型转换[(NSMutableDictionary *)[routenArray2 objectAtIndex:i] setObject:kennungString forKey:@"Kennung"];