Im Batman
Im Batman

Reputation: 1876

UITableViewCell - Updating NSMutableDictionary Values

I have UITableViewController and it has twitter like post with like buttons. what im doing is when ever like button clicked if it is success trying to update the like count by + 1. i did the all the the above method except for the update part. im getting Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object' Error.

Here is my code.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FeedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];


    NSDictionary *feedList = [liveFeeds objectAtIndex:indexPath.row];
//liveFeeds is NSMutableArray

cell.likeCount.text = [feedList objectForKey:@"likes"];
cell.like.tag = indexPath.row;

    [cell.like addTarget:self action:@selector(likeClicked:) forControlEvents:UIControlEventTouchUpInside];
}

-(void) likeClicked:(UIButton*)sender{

//Here im using AFNetworking and getting JSON response.
//After that im doing following to update the like

NSMutableDictionary* feedList = [liveFeeds objectAtIndex:sender.tag];

            NSString *oldLike = [feedList objectForKey:@"likes"];
            int newLike = [oldLike intValue] + 1;
            NSString *strFromInt = [NSString stringWithFormat:@"%d",newLike];

            NSLog(@"updated like %@",strFromInt); // up to this it works

            [feedList setObject:strFromInt forKey:@"likes"]; // in here it get crashe

            [self.tableView reloadData];

}

What i want to do is. liveFeeds Array update with that Like Count and reload the table. am i doing this wrong? or is there any easy way to do this?

Upvotes: 2

Views: 56

Answers (3)

technerd
technerd

Reputation: 14504

Simply create mutable copy of NSDictionary. Actually NSMutableDictionary* feedList = [liveFeeds objectAtIndex:sender.tag] returns NSDictionary. So to make it editable you have to create another copy which is mutable by using mutableCopy. NSMutableDictionary* feedList = [[liveFeeds objectAtIndex:sender.tag]mutableCopy]

 -(void) likeClicked:(UIButton*)sender{

 //Here im using AFNetworking and getting JSON response.
 //After that im doing following to update the like

       NSMutableDictionary* feedList = [[liveFeeds objectAtIndex:sender.tag]mutableCopy];

        NSString *oldLike = [feedList objectForKey:@"likes"];
        int newLike = [oldLike intValue] + 1;
        NSString *strFromInt = [NSString stringWithFormat:@"%d",newLike];

        NSLog(@"updated like %@",strFromInt); // up to this it works

        [feedList setObject:strFromInt forKey:@"likes"]; // in here it get crashe

        [self.tableView reloadData];

 }

Upvotes: 1

Mário Cosme
Mário Cosme

Reputation: 323

I haven't tried it yet but i guess you're trying to use a NSMutableDictionary method on a NSDictionary. Try changing:

NSMutableDictionary* feedList = [liveFeeds objectAtIndex:sender.tag];

To:

NSDictionary* dic = [liveFeeds objectAtIndex:sender.tag]; NSMutableDictionary* feedList = [NSMutableDictionary alloc] initWithDictionary:dic]];

Upvotes: 1

Jörn Buitink
Jörn Buitink

Reputation: 2916

Most likely [liveFeeds objectAtIndex:sender.tag] is a NSDictionary, not an NSMutableDictionary. So you cannot change its content.

Build feedlist with the content of [liveFeeds objectAtIndex:sender.tag] :

NSMutableDictionary* feedlist = [NSMutableDictionary dictionaryWithDictionary:[liveFeeds objectAtIndex:sender.tag]]

Upvotes: 2

Related Questions