在NSMutableDictionary中插入NSMutableArray对象

时间:2014-11-11 13:21:49

标签: objective-c nsmutablearray nsmutabledictionary

在我的应用程序中,我需要在NSMutableDictionary中插入NSMutableArray的成员。我的数组如下:

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

现在我需要创建一个类似于以下结构的NSMutableDictionary:

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"

有没有人可以帮我找到解决方案来做这些事情? 谢谢!

更新

我尝试使用trojanfoe建议我的代码并且它有效,但它并不完全是我需要的。我通过解析CSV文件将数据存储在我的NSMutableArray中。 CSV文件具有以下结构:

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

如果我使用tojanfoe向我建议的代码,我可以创建一个NSMutableDictionary,我只保存CSV的第一行,现在我如何对我的CSV的所有行执行相同的操作?

更新2

我以不同的方式制作,这是我的代码:

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

我疯了:当我执行我在这里发布的代码时,它为我创建了一个字典数组,但是在这个数组中,我总是看到相同的字典。 我试图通过使用断点来执行它,我看到j = 1它在我创建词典期间更改了我的finalArray,为什么在执行指令finalArray之前修改了我的[finalArray addObject:rowDataDict];

1 个答案:

答案 0 :(得分:2)

它可以帮助你

    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"]);
    }