user2289379
user2289379

Reputation:

How can i remove array of dictionary from NSMutableDictionary?

I have Dictionary of Array (inside that, also array and dictionary).

I added UITableViewCellEditingStyleDelete in my UITableView, so I want to remove specific array of dictionary from dictionary.

Console view of data:

{
        A =     (
                   {
                    address = "Talala (gir)";
                    "main_id" = 1;
                    mobile = 8878876884;
                    name = "Amit Patel";
                   },
                   {
                    address = "Junagdh";
                    "main_id" = 5;
                    mobile = 4894679865;
                    name = "Arjun Patel";
                   }
                );
            J = (
                    {
                    address = "Taveli";
                    "main_id" = 6;
                    mobile = 87886356085878;
                    name = "Jasmin Patel";
                    },
                    {
                    address = "Gujarat";
                    "main_id" = 4;
                    mobile = 6636633368;
                    name = "Jatin ";
                   }
               );
            R =     (
                        {
                    address = "Mumbai";
                    "main_id" = 2;
                    mobile = 999686322;
                    name = "Rajan Patel";
                }
            );
            S =     (
                        {
                    address = "Rajkot";
                    "main_id" = 3;
                    mobile = 8866086549;
                    name = "Sumit Patel";
                }
            );
        }

I want to delete for example: index of array is 1 from dictionary with key A

I have to tried with following code

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {

        NSDictionary *dic = [[self.finalPDic objectForKey:[self.listOfHeader objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

        NSLog(@"%@",dic);

        [self.finalPDic removeObjectForKey:dic];

        NSLog(@"%@",self.finalPDic);

        [self.tblView reloadData];
    }
    if (editingStyle == UITableViewCellEditingStyleInsert)
    {
    }
}

But i can not delete any record from main NSMutableDictionary.

Upvotes: 0

Views: 2791

Answers (5)

Madhu
Madhu

Reputation: 994

follow the steps. take a MutableArray and copy the entire array from your dictionary.

NSMutableArray *tempArray = [NSMutableArray arrayWithArray:[self.finalPDic objectForKey:  [self.listOfHeader objectAtIndex:indexPath.section]]];

now remove the entire object from dictionary for the key(store the key somewhere NSString *key = [self.listOfHeader objectAtIndex:indexPath.section])

  [self.finalPDic  removeObjectForKey:[self.listOfHeader objectAtIndex:indexPath.section]];

from the tempArray remove the dictionary which u want based on indexPath.row

    [tempArray removeObjectAtIndex:objectAtIndex:indexPath.row];

add the tempArray to mainDict with the saved key

 [self.finalPDic  setObject:tempArray forKey:key];//ie saved key.

hope this helps, i tried.

Upvotes: 0

rptwsthi
rptwsthi

Reputation: 10172

First of all make sure that each component of your data structure is mutable, (i.e. base dictionary, internal array, not mandatory but dictionary inside array as well that).

Then finally replace your commitEditingStyle method with this.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  {
    if (editingStyle == UITableViewCellEditingStyleDelete) {            
        [[self.finalPDic objectForKey:[self.listOfHeader objectAtIndex:indexPath.section]] removeObjectAtIndex:indexPath.row];
        [tableView reloadData];
    }
}

Upvotes: 1

Anupdas
Anupdas

Reputation: 10201

Try this

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        NSString *key = self.listOfHeader[indexPath.section];

        NSMutableArray *users = [self.finalPDic[key] mutableCopy];

        [users removeObjectAtIndex:indexPath.row];
        if (users){
          self.finalDic[key] = users;
        }else{
        [self.finalDic removeObjectForKey:key];
        }
        [self.tblView reloadRowsAtIndexPaths:@[indexPath]
                                withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    if (editingStyle == UITableViewCellEditingStyleInsert){
    }
}

Upvotes: 2

Totumus Maximus
Totumus Maximus

Reputation: 7573

When you are using dictionaries of dictionaries and values that are arrays of dictionaries it can get quite confusing when and how to delete a single (or multiple) values from de main dictionary. The trick is to see every object seperate from the main dictionary and threat that value as a new array or dictionary.

The following is what you should do: For convenience I'm gonna assume your dictionary is looking like the example that you have given in your question.

NSDictionary *maindict = //get your maindict here

NSArray *arr = [maindict valueForKey:someKey]; //in your example the values of your maindict is always an array.

//the array is filled with more dictionaries, apparantly objects that seem like contact details
//if you want to find a single item that is in this array you should loop over the array here.

for(NSDictionary *dict in arr)
{
    if(/*retrieve a value from the dict that you want to compare with a preset value before this function*/)
    {
        //remove the dict from the array here (or save it for after the loop so that you wont get the error that you try to mutate the array while running it)
    }

}

//now that the dict/record/contact is deleted from your array you should set it back at the same place in the maindict.
[maindict setValue:arr /*this arr is the arr with the removed record*/ forKey:someKey]; //someKey is the same key as you used earlier

//you are finished now! the maindict is updated with the removed value. the only thing left is updating your tableview so that the record is also removed visibly
[yourtableview reloadData]; 

Upvotes: 0

Hermann Klecker
Hermann Klecker

Reputation: 14068

NSMutableArray * dictArray = [self.finalPDic objectForKey:indexPath.section];  
[dictArray removeObjectAtIndex:indexPath.row];

This assumes that finalPDict carrys the one of which you show the NSLog output in your question. And indexPath.section may evaluate to "A" and indexPath.row may evaluate to 1.

Upvotes: 0

Related Questions