NSMutableArray中的NSDictionary在我的for循环中发生了变化

时间:2014-03-28 10:12:37

标签: ios objective-c nsmutablearray nsdictionary

我有一系列的约会。预约可以持续多天。例如

Start --> 20/03/2014
END   --> 23/03/2014

但我想在3月20日,21日,22日和23日的日历视图中显示约会。所以我写了下面这个函数。

        NSMutableArray *arrAppoints = [[NSMutableArray alloc]init];
        for(Appointment *appointmentValue in matches2){
            NSDateComponents *componentsBegin = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:appointmentValue.app_start];
            NSDateComponents *componentsEnd = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:appointmentValue.app_end];

            if([componentsEnd day] - [componentsBegin day] == 0){
                //Appointment on same day
                UIColor *appColor = [appointmentValue getLabelbyId:[appointmentValue valueForKey:@"app_label_id"]];
                NSDictionary *dictAppointment = [[NSDictionary alloc]initWithObjectsAndKeys:appointmentValue,@"appointment",appColor,@"appColor", nil];
                [arrAppoints addObject:dictAppointment];
            }else{
                //Appointment is over multiple days
                NSInteger count = [componentsEnd day] - [componentsBegin day];
                UIColor *appColor = [appointmentValue getLabelbyId:[appointmentValue valueForKey:@"app_label_id"]];
                for (int i=0;i<=count;i++){
                    if (i==0){
                        //Store it in arrAppoints
                        NSDictionary *dictAppointment = [[NSDictionary alloc]initWithObjectsAndKeys:appointmentValue,@"appointment",appColor,@"appColor", nil];
                        //arrAppoints is the array that I pass through my dayview. (stores only the appointment for that day)
                        [arrAppoints addObject:dictAppointment];
                    }else{
                        //Store it in arrMultipleDays and get it later on the correct day
                        NSLog(@"arrDAYS 1 is %@",arrMultipleDays);
                        int daysToAdd = i;
                        NSDate *newDate1 = [appointmentValue.app_start dateByAddingTimeInterval:60*60*24*daysToAdd];
                        Appointment *newAppointment = appointmentValue;
                        newAppointment.app_start = newDate1;
                        NSLog(@"arrDAYS 1B is %@",arrMultipleDays);
                        newAppointment.app_fullday = [NSNumber numberWithInt:1];
                        NSNumber *iValue = [NSNumber numberWithInt:i];

                        NSDictionary *dictAppointment = [[NSDictionary alloc]initWithObjectsAndKeys:newAppointment,@"appointment",appColor,@"appColor",iValue,@"value", nil];
                        [arrMultipleDays addObject:dictAppointment];
                        NSLog(@"arrDAYS 2  is %@",arrMultipleDays);
                    }
                }
            }
        }

问题 当i = 1时,它会在其中添加NSDictionary约会。但是当i = 2时,它会增加第二个NSDictionary,但第一个NSDictionary中的约会被修改为与第二个约会相同的约会。

请查看下面的NSLOG并查看app_start

arrDAYS 1 is (
)
2014-03-28 11:10:52.818 Adsolut[3588:60b] arrDAYS 1B is (
)
2014-03-28 11:10:52.822 Adsolut[3588:60b] arrDAYS 2  is (
        {
        appColor = "UIDeviceRGBColorSpace 0.501961 1 1 1";
        appointment = "<Appointment: 0x15e947f0> (entity: Appointment; id: 0x15ee44c0 <x-coredata://9554A902-6185-40F7-B5FD-566F2FC479F8/Appointment/p31> ; data: {\n    \"app_addressinfo\" = \"\";\n    \"app_completed\" = 0;\n    \"app_delete\" = 0;\n    \"app_description\" = \"\";\n    \"app_end\" = \"2014-03-23 19:50:15 +0000\";\n    \"app_fullday\" = 1;\n    \"app_id\" = 297;\n    \"app_label\" = Opleiding;\n    \"app_label_id\" = 0;\n    \"app_location\" = \"nieuwe locatie\";\n    \"app_parentId\" = nil;\n    \"app_private\" = 0;\n    \"app_project_name\" = \"\";\n    \"app_project_number\" = \"\";\n    \"app_recurrenceInfo\" = \"\";\n    \"app_relation_address_city\" = \"\";\n    \"app_relation_address_id\" = \"-1\";\n    \"app_relation_address_name\" = \"\";\n    \"app_relation_address_street\" = \"\";\n    \"app_relation_code\" = \"\";\n    \"app_relation_contact_id\" = \"-1\";\n    \"app_relation_contact_name\" = \"\";\n    \"app_relation_name\" = \"\";\n    \"app_reminder_info\" = \"\";\n    \"app_resxPers\" = \"<ResourceIds> <ResourceId Type=\\\"System.String\\\" Value=\\\"SANMAX\\\" /> </ResourceIds>\";\n    \"app_start\" = \"2014-03-21 11:50:15 +0000\";\n    \"app_state\" = 2;\n    \"app_subject\" = \"nieuwe afspraak meerdere dagen\";\n    \"app_supplier_code\" = 000318;\n    \"app_supplier_contact_person_id\" = \"-1\";\n    \"app_supplier_contact_person_name\" = \"\";\n    \"app_supplier_name\" = appmax;\n    \"app_type\" = 0;\n    sectionIdentifier = 20140321;\n    synchronized = 1;\n})";
        value = 1;
    }
)
2014-03-28 11:10:52.825 Adsolut[3588:60b] arrDAYS 1 is (
        {
        appColor = "UIDeviceRGBColorSpace 0.501961 1 1 1";
        appointment = "<Appointment: 0x15e947f0> (entity: Appointment; id: 0x15ee44c0 <x-coredata://9554A902-6185-40F7-B5FD-566F2FC479F8/Appointment/p31> ; data: {\n    \"app_addressinfo\" = \"\";\n    \"app_completed\" = 0;\n    \"app_delete\" = 0;\n    \"app_description\" = \"\";\n    \"app_end\" = \"2014-03-23 19:50:15 +0000\";\n    \"app_fullday\" = 1;\n    \"app_id\" = 297;\n    \"app_label\" = Opleiding;\n    \"app_label_id\" = 0;\n    \"app_location\" = \"nieuwe locatie\";\n    \"app_parentId\" = nil;\n    \"app_private\" = 0;\n    \"app_project_name\" = \"\";\n    \"app_project_number\" = \"\";\n    \"app_recurrenceInfo\" = \"\";\n    \"app_relation_address_city\" = \"\";\n    \"app_relation_address_id\" = \"-1\";\n    \"app_relation_address_name\" = \"\";\n    \"app_relation_address_street\" = \"\";\n    \"app_relation_code\" = \"\";\n    \"app_relation_contact_id\" = \"-1\";\n    \"app_relation_contact_name\" = \"\";\n    \"app_relation_name\" = \"\";\n    \"app_reminder_info\" = \"\";\n    \"app_resxPers\" = \"<ResourceIds> <ResourceId Type=\\\"System.String\\\" Value=\\\"SANMAX\\\" /> </ResourceIds>\";\n    \"app_start\" = \"2014-03-21 11:50:15 +0000\";\n    \"app_state\" = 2;\n    \"app_subject\" = \"nieuwe afspraak meerdere dagen\";\n    \"app_supplier_code\" = 000318;\n    \"app_supplier_contact_person_id\" = \"-1\";\n    \"app_supplier_contact_person_name\" = \"\";\n    \"app_supplier_name\" = appmax;\n    \"app_type\" = 0;\n    sectionIdentifier = 20140321;\n    synchronized = 1;\n})";
        value = 1;
    }
)
2014-03-28 11:10:52.829 Adsolut[3588:60b] arrDAYS 1B is (
        {
        appColor = "UIDeviceRGBColorSpace 0.501961 1 1 1";
        appointment = "<Appointment: 0x15e947f0> (entity: Appointment; id: 0x15ee44c0 <x-coredata://9554A902-6185-40F7-B5FD-566F2FC479F8/Appointment/p31> ; data: {\n    \"app_addressinfo\" = \"\";\n    \"app_completed\" = 0;\n    \"app_delete\" = 0;\n    \"app_description\" = \"\";\n    \"app_end\" = \"2014-03-23 19:50:15 +0000\";\n    \"app_fullday\" = 1;\n    \"app_id\" = 297;\n    \"app_label\" = Opleiding;\n    \"app_label_id\" = 0;\n    \"app_location\" = \"nieuwe locatie\";\n    \"app_parentId\" = nil;\n    \"app_private\" = 0;\n    \"app_project_name\" = \"\";\n    \"app_project_number\" = \"\";\n    \"app_recurrenceInfo\" = \"\";\n    \"app_relation_address_city\" = \"\";\n    \"app_relation_address_id\" = \"-1\";\n    \"app_relation_address_name\" = \"\";\n    \"app_relation_address_street\" = \"\";\n    \"app_relation_code\" = \"\";\n    \"app_relation_contact_id\" = \"-1\";\n    \"app_relation_contact_name\" = \"\";\n    \"app_relation_name\" = \"\";\n    \"app_reminder_info\" = \"\";\n    \"app_resxPers\" = \"<ResourceIds> <ResourceId Type=\\\"System.String\\\" Value=\\\"SANMAX\\\" /> </ResourceIds>\";\n    \"app_start\" = \"2014-03-23 11:50:15 +0000\";\n    \"app_state\" = 2;\n    \"app_subject\" = \"nieuwe afspraak meerdere dagen\";\n    \"app_supplier_code\" = 000318;\n    \"app_supplier_contact_person_id\" = \"-1\";\n    \"app_supplier_contact_person_name\" = \"\";\n    \"app_supplier_name\" = appmax;\n    \"app_type\" = 0;\n    sectionIdentifier = 20140321;\n    synchronized = 1;\n})";
        value = 1;
    }
)
2014-03-28 11:10:52.834 Adsolut[3588:60b] arrDAYS 2  is (
        {
        appColor = "UIDeviceRGBColorSpace 0.501961 1 1 1";
        appointment = "<Appointment: 0x15e947f0> (entity: Appointment; id: 0x15ee44c0 <x-coredata://9554A902-6185-40F7-B5FD-566F2FC479F8/Appointment/p31> ; data: {\n    \"app_addressinfo\" = \"\";\n    \"app_completed\" = 0;\n    \"app_delete\" = 0;\n    \"app_description\" = \"\";\n    \"app_end\" = \"2014-03-23 19:50:15 +0000\";\n    \"app_fullday\" = 1;\n    \"app_id\" = 297;\n    \"app_label\" = Opleiding;\n    \"app_label_id\" = 0;\n    \"app_location\" = \"nieuwe locatie\";\n    \"app_parentId\" = nil;\n    \"app_private\" = 0;\n    \"app_project_name\" = \"\";\n    \"app_project_number\" = \"\";\n    \"app_recurrenceInfo\" = \"\";\n    \"app_relation_address_city\" = \"\";\n    \"app_relation_address_id\" = \"-1\";\n    \"app_relation_address_name\" = \"\";\n    \"app_relation_address_street\" = \"\";\n    \"app_relation_code\" = \"\";\n    \"app_relation_contact_id\" = \"-1\";\n    \"app_relation_contact_name\" = \"\";\n    \"app_relation_name\" = \"\";\n    \"app_reminder_info\" = \"\";\n    \"app_resxPers\" = \"<ResourceIds> <ResourceId Type=\\\"System.String\\\" Value=\\\"SANMAX\\\" /> </ResourceIds>\";\n    \"app_start\" = \"2014-03-23 11:50:15 +0000\";\n    \"app_state\" = 2;\n    \"app_subject\" = \"nieuwe afspraak meerdere dagen\";\n    \"app_supplier_code\" = 000318;\n    \"app_supplier_contact_person_id\" = \"-1\";\n    \"app_supplier_contact_person_name\" = \"\";\n    \"app_supplier_name\" = appmax;\n    \"app_type\" = 0;\n    sectionIdentifier = 20140321;\n    synchronized = 1;\n})";
        value = 1;
    },
        {
        appColor = "UIDeviceRGBColorSpace 0.501961 1 1 1";
        appointment = "<Appointment: 0x15e947f0> (entity: Appointment; id: 0x15ee44c0 <x-coredata://9554A902-6185-40F7-B5FD-566F2FC479F8/Appointment/p31> ; data: {\n    \"app_addressinfo\" = \"\";\n    \"app_completed\" = 0;\n    \"app_delete\" = 0;\n    \"app_description\" = \"\";\n    \"app_end\" = \"2014-03-23 19:50:15 +0000\";\n    \"app_fullday\" = 1;\n    \"app_id\" = 297;\n    \"app_label\" = Opleiding;\n    \"app_label_id\" = 0;\n    \"app_location\" = \"nieuwe locatie\";\n    \"app_parentId\" = nil;\n    \"app_private\" = 0;\n    \"app_project_name\" = \"\";\n    \"app_project_number\" = \"\";\n    \"app_recurrenceInfo\" = \"\";\n    \"app_relation_address_city\" = \"\";\n    \"app_relation_address_id\" = \"-1\";\n    \"app_relation_address_name\" = \"\";\n    \"app_relation_address_street\" = \"\";\n    \"app_relation_code\" = \"\";\n    \"app_relation_contact_id\" = \"-1\";\n    \"app_relation_contact_name\" = \"\";\n    \"app_relation_name\" = \"\";\n    \"app_reminder_info\" = \"\";\n    \"app_resxPers\" = \"<ResourceIds> <ResourceId Type=\\\"System.String\\\" Value=\\\"SANMAX\\\" /> </ResourceIds>\";\n    \"app_start\" = \"2014-03-23 11:50:15 +0000\";\n    \"app_state\" = 2;\n    \"app_subject\" = \"nieuwe afspraak meerdere dagen\";\n    \"app_supplier_code\" = 000318;\n    \"app_supplier_contact_person_id\" = \"-1\";\n    \"app_supplier_contact_person_name\" = \"\";\n    \"app_supplier_name\" = appmax;\n    \"app_type\" = 0;\n    sectionIdentifier = 20140321;\n    synchronized = 1;\n})";
        value = 2;
    }
)

2 个答案:

答案 0 :(得分:2)

将循环中的Appointment *newAppointment = appointmentValue;更改为

 Appointment *newAppointment = [[Appointment alloc] init];

分配newAppointment = appointmentValue;时,每次更改都会对相同的实例进行编辑。

答案 1 :(得分:2)

我假设您使用appointmentValue作为起点,以便复制相似的值,例如颜色?然后,您需要使用copy

Appointment *newAppointment = [appointmentValue copy];

您可能必须实现自己的&#39; copyWithZone:`以确保正确复制所有属性。

或者正如Akhilrajtr建议分配新版本并复制所需的所有属性