将数据添加到另一个NSDictionary中的NSDictionary

时间:2016-12-24 07:05:28

标签: ios objective-c iphone nsdictionary

我正在尝试将数据添加到嵌套在其他词典中的NSMutableDictionary

在添加数据的代码运行之前启动输出

DayData Dictionary {            //DayData Dictionary

Monday =     {                  //events Dictionary
    trip1 =         {           //eventData Dictionary
        numItems = 9items;
        time = "5:00";
        tripName = beachfucking;
    };
};
Thursday =     {
    trip1 =         {
        numItems = 9items;
        time = "5:00";
        tripName = beachfucking;
    };
};
Tuesday =     {
    trip1 =         {
        numItems = 9items;
        time = "5:00";
        tripName = beachfucking;
    };
};
Wendsday =     {
    trip1 =         {
        numItems = 9items;
        time = "5:00";
        tripName = beachfucking;
    };
};
}

生成此输出的代码

NSMutableDictionary *eventData = [[NSMutableDictionary alloc]initWithObjects:@[@"5:00",@"9items",@"beachfucking"] forKeys:@[@"time",@"numItems",@"tripName"]];

NSMutableDictionary *event = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                              eventData,@"trip1",nil];

NSMutableDictionary *dayData = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        event,@"Monday",event,@"Tuesday",
                                        event,@"Wendsday",event,@"Thursday",nil];

我目前正尝试解决问题。在这里,我创建一个新事件NSDictionary,然后将该数据添加到DayData字典。但输出不正确。一周中的每一天都会得到一次" trip2"星期一只假设的数据集。

 NSMutableDictionary *event2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                              eventData,@"trip2",nil];
[[dayData objectForKey:@"Monday"] addEntriesFromDictionary:event2];

以上代码的当前错误输出

2016-12-24 01:56:41.261329 test1[10098:380349] {
    Monday =     {
        trip1 =         {
            numItems = 9items;
            time = "5:00";
            tripName = beachfucking;
        };
        trip2 =         {
            numItems = 9items;
            time = "5:00";
            tripName = beachfucking;
        };
    };
    Thursday =     {
        trip1 =         {
            numItems = 9items;
            time = "5:00";
            tripName = beachfucking;
        };
        trip2 =         {
            numItems = 9items;
            time = "5:00";
            tripName = beachfucking;
        };
    };
    Tuesday =     {
        trip1 =         {
            numItems = 9items;
            time = "5:00";
            tripName = beachfucking;
        };
        trip2 =         {
            numItems = 9items;
            time = "5:00";
            tripName = beachfucking;
        };
    };
    Wendsday =     {
        trip1 =         {
            numItems = 9items;
            time = "5:00";
            tripName = beachfucking;
        };
        trip2 =         {
            numItems = 9items;
            time = "5:00";
            tripName = beachfucking;
        };
    };
    }

请注意trip2如何填写一周的所有日子。

1 个答案:

答案 0 :(得分:2)

您的所有日期密钥都包含相同的参考对象event,因此更改其中一个密钥将反映所有日期密钥。

要解决此问题,请按此声明dayData Dictionary

NSMutableDictionary *dayData = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                [event mutableCopy] ,@"Monday",[event mutableCopy],@"Tuesday",
                                [event mutableCopy],@"Wendsday",[event mutableCopy],@"Thursday",nil];