gpt4 book ai didi

iphone - 迭代充满日期的 NSArray 并与另一个 NSArray 进行比较

转载 作者:行者123 更新时间:2023-12-03 21:18:40 28 4
gpt4 key购买 nike

我有一个 NSArray,其中填充了大约 30 个 NSDate 格式的日期,

我需要做的是从中创建另一个数组,其中包含从第一个日期到最后一个日期的所有日期的 bool 值。

前数组 111/1/12011 年 3 月 1 日2011 年 5 月 1 日

数组 2是的不是的不是的

我需要这个用于 Tapku 图书馆日历

这是我到目前为止所拥有的,但我从未改变

int i=0;
for (NSDate *date = [[startingDate copy] autorelease]; [date compare: endingDate] < 0;
date = [date dateByAddingTimeInterval:24 * 60 * 60] ) {
NSLog( @"%@ in [%@,%@]", date, startingDate, endingDate );

int day1 = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:[eventDates objectAtIndex:i]];
int day2 = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:date];

if(day1-day2==0) {
NSLog(@"yeh");
i=i+1;
//add yes to array2

} else {
NSLog(@"nah");
NSLog(@"%i",i);
//add no to array 2
}
}

最佳答案

我想我不太明白你想做什么,但我会尝试一下。

假设您有 2 NSDate的,调用他们date1date2 ,并且您想要标记 NSDate 数组中的任何条目的,称之为arrayOfDates ,介于 2 NSDate 之间或不,并存储 YESNO另一个数组中的值,将其称为 boolValuesOfDates 。那么试试这个方法:

- (void)compareDatesOfArrayFromDate:(NSDate *)date1 toDate:(NSDate *)date2; {

NSDate * firstDate;
NSDate * secondDate;

if([date1 timeIntervalSinceDate:date2] >= 0){
firstDate = date2;
secondDate = date1;
}
else{
firstDate = date1;
secondDate = date2;
}

if(boolValuesOfDates != nil)
[boolValuesOfDates release]; // Release the YES/NO array for the new values.

boolValuesOfDates = [[NSMutableArray alloc] initWithCapacity:[arrayOfDates count]];

BOOL isInbetweenDates;
NSDate * curDate;

for(int i = 0; i < [arrayOfDates count]; i++){
curDate = [arrayOfDates objectAtIndex:i];
isInbetweenDates = NO;
if([curDate timeIntervalSinceDate:firstDate] >= 0){
if([curDate timeIntervalSinceDate:secondDate] <= 0){
isInbetweenDates = YES;
}
}
[boolValuesOfDates addObject:[NSNumber numberWithBool:isInbetweenDates]];
}
}

此方法检查输入的 2 NSDate 的顺序的,然后比较 NSDate位于数组中,如果它们位于数组之间,则用 YES 标记它们,或NO否则。希望有帮助!

关于iphone - 迭代充满日期的 NSArray 并与另一个 NSArray 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7218510/

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