如何获得两个NSDateComponents之间的NSDateComponents?
我有两个NSDateComponents
,我想在这两个之间的所有NSDateComponents
,我已经尝试了以下,
NSDateComponents *first = ...; NSDateComponents *second = ...; BOOL boolDone = NO; while (!boolDone) { [array addObject:first]; first.day+=1; NSLog(@"%@",first); if([[first date] compare:[second date]] == NSOrderedSame) { boolDone = YES; } } NSLog(@"All dates : %@",array);
循环后,它只是打印我在第一个 NSDateComponent
的date…! 怎么了?
这是一个要理解的日志
2014-01-18 19:47:16.413 testCalendar [4274:a0b]
Calendar Year: 2014 Month: 1 Leap month: no Day: 19
2014-01-18 19:47:16.415 testCalendar [4274:a0b]
Calendar Year: 2014 Month: 1 Leap month: no Day: 20
2014-01-18 19:47:16.416 testCalendar [4274:a0b]
Calendar Year: 2014 Month: 1 Leap month: no Day: 21
2014-01-18 19:47:16.416 testCalendar [4274:a0b]
Calendar Year: 2014 Month: 1 Leap month: no Day: 22
2014-01-18 19:47:16.417 testCalendar [4274:a0b]
Calendar Year: 2014 Month: 1 Leap month: no Day: 23
2014-01-18 19:47:16.418 testCalendar [4274:a0b]
23-1-2014
23-1-2014
23-1-2014
23-1-2014
23-1-2014
你总是添加相同的元素到数组中。 数组只保存指向其元素的指针 ,因此在循环结束时,它首先包含n个指向同一对象的指针。 更改
[array addObject:first];
至
[array addObject:[first copy]];
应该解决问题。