Pregunta

I am trying to add NSMutableDictionary into NSMutableArray on a specific index

The NSMutableArray is already filled the data is coming from the webservice, but i want

to add another dictionary in my array on a 0 index.

But I'm kind of stuck here :

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: ' -[JKArray insertObject:atIndex:]: mutating method sent to immutable object'

My code is:

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];

}

Please Help

Thanks.

¿Fue útil?

Solución

You should try using arrayWithObjects. You are not going on the right track. I am unable to understand the usage of [self.cityArray objectAtIndex:0];

Try this:

 NSDictionary *dict = [[NSDictionary alloc] init];
 NSMutableArray *cities = [[NSMutableArray alloc] initWithArray:[dict objectForKey:@"cities"]];

I hope this will help.

Otros consejos

In case your cityArray already having an object, then try this:

[cityArray insertObject:[self.dealsDict objectForKey:@"Cities"] atIndex:0];

But your cityArray is empty then try this:

[cityArray addObject:[self.dealsDict objectForKey:@"Cities"]]
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top