Insert NSMutableArray object in a NSMutableDictionary

97 Views Asked by At

In my app I need to insert member of a NSMutableArray in a NSMutableDictionary. My array is like the follow:

[Entity_ID] [Lastname] [Firstname] [Email] [Street] [StreetNr] [Zipcode] [City] [Opt] [Gender]  [Age] [Clicks] [Transactions]

Now I need to create a NSMutableDictionary like the following structure:

key = "ID" value = "Entity_ID"
key = "Lastname" value = "Lastname"
key = "Firstname" value = "Firstname"
key = "Email" value = "Email"
key = "Street" value = "Street"
key = "StreetNr" value = "StreetNr"
key = "Zipcode" value = "Zipcode"
key = "City" value = "City"
key = "Opt" value = "Opt"
key = "Gender" value = "Gender"
key = "Age" value = "Age"
key = "Clicks" value = "Clicks"
key = "Transactions" value = "Transactions"

Does anyone can help me to find a solution to do this stuff? Thank you!

UPDATE

I tried to use the code suggest me by trojanfoe and it works, but it's not exactly what I need. I get he data stored in my NSMutableArray by parsing a CSV file. The CSV file have the following structure:

Entity_ID   Lastname    Firstname   Email   Street  StreetNr    Zipcode City    Opt Gender  Age Clicks  Transactions
ID          Surname     Name        mail    address number      zip     city    x   female  xy  z       k
ID          Surname     Name        mail    address number      zip     city    x   female  xy  z       k
ID          Surname     Name        mail    address number      zip     city    x   female  xy  z       k
ID          Surname     Name        mail    address number      zip     city    x   female  xy  z       k
ID          Surname     Name        mail    address number      zip     city    x   female  xy  z       k
ID          Surname     Name        mail    address number      zip     city    x   female  xy  z       k
ID          Surname     Name        mail    address number      zip     city    x   female  xy  z       k

If I use the code that tojanfoe suggested to me, I can create a NSMutableDictionary in which I save only the first row of my CSV, now how I can do the same for all the row of my CSV?

UPDATE 2

I made in a different way, this is my code:

 NSMutableArray *finalArray = [[NSMutableArray alloc]init];

    NSMutableDictionary *rowDataDict = [[NSMutableDictionary alloc]init];

    int i = 0;
    int k = 0;

    for (int j = 0; j < cleanedArray.count; j++) {
        while (i < 11) {
            if (k < cleanedArray.count) {
                NSLog(@"%@", finalArray);
                 NSLog(@"k = %d, i = %d", i, k);
                [rowDataDict setObject:cleanedArray[k] forKey:@"Lastname"];
                k++;
                i++;
                NSLog(@"%@", finalArray);
                NSLog(@"k = %d, i = %d", i, k);
                [rowDataDict setObject:cleanedArray[k] forKey:@"Firstname"];
                k++;
                i++;
                NSLog(@"%@", finalArray);
                NSLog(@"k = %d, i = %d", i, k);
                [rowDataDict setObject:cleanedArray[k] forKey:@"Email"];
                k++;
                i++;
                NSLog(@"%@", finalArray);
                NSLog(@"k = %d, i = %d", i, k);
                [rowDataDict setObject:cleanedArray[k] forKey:@"Street"];
                k++;
                i++;
                NSLog(@"%@", finalArray);
                NSLog(@"k = %d, i = %d", i, k);
                [rowDataDict setObject:cleanedArray[k] forKey:@"StreetNr"];
                k++;
                i++;
                NSLog(@"%@", finalArray);
                NSLog(@"k = %d, i = %d", i, k);
                [rowDataDict setObject:cleanedArray[k] forKey:@"Zipcode"];
                k++;
                i++;
                NSLog(@"%@", finalArray);
                NSLog(@"k = %d, i = %d", i, k);
                [rowDataDict setObject:cleanedArray[k] forKey:@"City"];
                k++;
                i++;
                NSLog(@"%@", finalArray);
                NSLog(@"k = %d, i = %d", i, k);
                [rowDataDict setObject:cleanedArray[k] forKey:@"Opt"];
                k++;
                i++;
                NSLog(@"%@", finalArray);
                NSLog(@"k = %d, i = %d", i, k);
                [rowDataDict setObject:cleanedArray[k] forKey:@"Gender"];
                k++;
                i++;
                NSLog(@"%@", finalArray);
                NSLog(@"k = %d, i = %d", i, k);
                [rowDataDict setObject:cleanedArray[k] forKey:@"Age"];
                k++;
                i++;
                NSLog(@"%@", finalArray);
                NSLog(@"k = %d, i = %d", i, k);
                [rowDataDict setObject:cleanedArray[k] forKey:@"Clicks"];
                k++;
                i++;
                NSLog(@"%@", finalArray);
                NSLog(@"k = %d, i = %d", i, k);
                [rowDataDict setObject:cleanedArray[k] forKey:@"Transactions"];
                NSLog(@"%@", finalArray);
            }

        }
        [finalArray addObject:rowDataDict];
        NSLog(@"%@", finalArray);
        i = 0;
    }

    NSLog(@"%@", finalArray);

And I'm getting crazy: when I execute the code I posted on here it create for me an array of dictionaries, but in this array i saw always the same dictionary. I tried to execute it by using breakpoints and I see for j = 1 it changes my finalArray during I create the dictionaries, why it modify my finalArray before doing the instructions [finalArray addObject:rowDataDict];

1

There are 1 best solutions below

0
On

It Might Help you

    NSMutableDictionary *info = [[NSMutableDictionary alloc] init];
    [info setObject:@"sumit" forKey:@"name"];
    [info setObject:@"22" forKey:@"age"];

    NSMutableDictionary *info1 = [[NSMutableDictionary alloc] init];
    [info1 setObject:@"golu" forKey:@"name"];
    [info1 setObject:@"26" forKey:@"age"];

    NSMutableDictionary *info3 = [[NSMutableDictionary alloc] init];
    [info3 setObject:@"Bholu" forKey:@"name"];
    [info3 setObject:@"20" forKey:@"age1"];

    NSMutableArray *allInfor = [[NSMutableArray alloc] init];
    [allInfor addObject:info];
    [allInfor addObject:info1];
    [allInfor addObject:info3];

    for (int i = 0; i<[allInfor count]; i++) {
        NSMutableDictionary *dicinfo = [allInfor objectAtIndex:i];
        NSLog(@"name is :- %@, age is:- %@",[dicinfo valueForKey:@"name"], [dicinfo valueForKey:@"age"@"age1"]);
    }