gpt4 book ai didi

ios - NSMutableArray 中的 NSDictionary 在我的整个 for 循环中发生变化

转载 作者:行者123 更新时间:2023-11-28 19:03:28 26 4
gpt4 key购买 nike

我有一组约会。约会可以持续多天。例如

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

但我想在我的日历 View 中显示 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;
}
)

最佳答案

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

Appointment *newAppointment = [appointmentValue copy];

您可能必须实现自己的“copyWithZone:”以确保正确复制所有属性。

或者正如 Akhilrajtr 建议的那样,分配一个新版本并复制您需要的所有属性

关于ios - NSMutableArray 中的 NSDictionary 在我的整个 for 循环中发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22709819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com