我试图在特定索引
上将NSMutableDictionary添加到NSMutableArray中NSMutableArray已填充数据来自webservice,但我想要
在我的数组中在0索引上添加另一个字典。
但我有点困在这里:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: ' -[JKArray insertObject:atIndex:]: mutating method sent to immutable object'
我的代码是:
on .h file
@property (strong, nonatomic)NSMutableArray *cityArray;
@property (strong, nonatomic)NSMutableDictionary *cityDict;
.m file:
- (void)viewDidLoad
{
[super viewDidLoad];
self.cityArray = [[NSMutableArray alloc]init];
self.cityDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"All Cities",@"key1", nil];
NSLog(@"City Dict=> %@", cityDict);
}
-(void)ConnectionDidFinishLoading:(NSString *)responseString :(NSString *)serviceName{
self.dealsDict = [responseString objectFromJSONString];
self.cityArray = [self.dealsDict objectForKey:@"Cities"];
[self.cityArray addObject:[self.cityDict copy]];
[self.cityArray addObject:self.cityDict];
[self.cityArray objectAtIndex:0];
[self.tableView reloadData];
}
请帮助
感谢。
答案 0 :(得分:5)
您应该尝试使用arrayWithObjects
。你没有走上正确的轨道。
我无法理解[self.cityArray objectAtIndex:0];
试试这个:
NSDictionary *dict = [[NSDictionary alloc] init];
NSMutableArray *cities = [[NSMutableArray alloc] initWithArray:[dict objectForKey:@"cities"]];
我希望这会有所帮助。
答案 1 :(得分:0)
如果您的cityArray
已有对象,请尝试以下操作:
[cityArray insertObject:[self.dealsDict objectForKey:@"Cities"] atIndex:0];
但是你的cityArray is
为空,请尝试以下方法:
[cityArray addObject:[self.dealsDict objectForKey:@"Cities"]]