Iphone 如何检查NSDate是否位于NSMutableArray中的其他两个NSDate之间

Iphone 如何检查NSDate是否位于NSMutableArray中的其他两个NSDate之间,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,在我的应用程序中,我有一个NSMutableArray,用户可以通过添加或删除数组中的条目来修改它(顺便说一句,条目总是添加在索引0处),这是通过表视图完成的。数组存储区中的每个条目(作为NSString添加单元格的日期)的格式如下:即@“Sat,2011年3月12日”。假设我还创建了一个变量NSString*myDay=@“Thu” 我的问题是如何检查存储在索引0中的日期和存储在索引1中的日期之间,myDay表示的日期是否丢失或不在这两个日期实体之间。在我的例子中,我只需要通过比较数组的索引0

在我的应用程序中,我有一个NSMutableArray,用户可以通过添加或删除数组中的条目来修改它(顺便说一句,条目总是添加在索引0处),这是通过表视图完成的。数组存储区中的每个条目(作为NSString添加单元格的日期)的格式如下:即
@“Sat,2011年3月12日”
。假设我还创建了一个变量
NSString*myDay=@“Thu”

我的问题是如何检查存储在索引0中的日期和存储在索引1中的日期之间,myDay表示的日期是否丢失或不在这两个日期实体之间。在我的例子中,我只需要通过比较数组的索引0和1来进行检查


还请注意,在我的应用程序中,变量myDay不是一个特定的日期(即“Thu,Mar 10,2011”,它只代表用户选择的一周中的一天,因为我的应用程序中的一些数据需要每周重置。

NSDateComponents和NSCalendar允许您在NSDates上执行这种逻辑


如果应用程序中的日期建模为字符串,则需要首先将其转换为NSDates。最好将日期建模为NSDates,并使用格式化程序将其转换为字符串以显示在表中。

NSDateComponents和NSCalendar允许您在NSDates上执行此类逻辑


如果应用程序中的日期建模为字符串,则需要先将其转换为NSDates。最好将日期建模为NSDates,并使用格式设置程序将其转换为字符串以在表中显示。

您可以将日期放入数组并对该数组进行排序。然后检查这些不同日期的索引是否正确一个日期介于其他日期之间:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *comps1 = [[NSDateComponents alloc] init];
NSDateComponents *comps2 = [[NSDateComponents alloc] init];
NSDateComponents *comps3 = [[NSDateComponents alloc] init];

[comps1 setDay:10];
[comps2 setDay:12];
[comps3 setDay:11];

NSDate *day1 = [gregorian dateByAddingComponents:comps1 toDate:[NSDate date] options:0];
NSDate *day2 = [gregorian dateByAddingComponents:comps2 toDate:[NSDate date] options:0];
NSDate *day3 = [gregorian dateByAddingComponents:comps3 toDate:[NSDate date] options:0];

NSMutableArray *array = [NSMutableArray arrayWithObjects:day1, day2, day3, nil];


[array sortUsingSelector:@selector(compare:)];

NSUInteger indexOfDay1 = [array indexOfObject:day1];
NSUInteger indexOfDay2 = [array indexOfObject:day2];
NSUInteger indexOfDay3 = [array indexOfObject:day3];

if (((indexOfDay1 < indexOfDay2 ) && (indexOfDay2 < indexOfDay3)) || 
    ((indexOfDay1 > indexOfDay2 ) && (indexOfDay2 > indexOfDay3))) {
    NSLog(@"YES");
} else {
    NSLog(@"NO");
}



[comps1 release];
[comps2 release];
[comps3 release];
[gregorian release];
NSCalendar*gregorian=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents*comps1=[[NSDateComponents alloc]init];
NSDateComponents*comps2=[[NSDateComponents alloc]init];
NSDateComponents*comps3=[[NSDateComponents alloc]init];
[COMS1设定日:10];
[比较2设定日:12];
[比较3设定日:11];
NSDate*day1=[gregorian dateByAddingComponents:comps1 toDate:[NSDate date]选项:0];
NSDate*day2=[gregorian dateByAddingComponents:comps2 toDate:[NSDate date]选项:0];
NSDate*day3=[gregorian dateByAddingComponents:comps3 toDate:[NSDate date]选项:0];
NSMUTABLEARRY*array=[NSMUTABLEARRY arrayWithObjects:day1,day2,day3,nil];
[数组排序选择器:@选择器(比较:)];
NSUTEGER indexOfDay1=[array indexOfObject:day1];
NSUTEGER indexOfDay2=[array indexOfObject:day2];
NSUTEGER indexOfDay3=[array indexOfObject:day3];
如果((indexOfDay1indexOfDay2)和&(indexOfDay2>indexOfDay3))){
NSLog(@“是”);
}否则{
NSLog(@“否”);
}
[comps1发布];
[comps2发布];
[comps3发布];
[公历释放];

您可以将日期放入一个数组并对该数组进行排序。然后检查这些不同日期的索引,以查看一个日期是否位于其他日期之间:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *comps1 = [[NSDateComponents alloc] init];
NSDateComponents *comps2 = [[NSDateComponents alloc] init];
NSDateComponents *comps3 = [[NSDateComponents alloc] init];

[comps1 setDay:10];
[comps2 setDay:12];
[comps3 setDay:11];

NSDate *day1 = [gregorian dateByAddingComponents:comps1 toDate:[NSDate date] options:0];
NSDate *day2 = [gregorian dateByAddingComponents:comps2 toDate:[NSDate date] options:0];
NSDate *day3 = [gregorian dateByAddingComponents:comps3 toDate:[NSDate date] options:0];

NSMutableArray *array = [NSMutableArray arrayWithObjects:day1, day2, day3, nil];


[array sortUsingSelector:@selector(compare:)];

NSUInteger indexOfDay1 = [array indexOfObject:day1];
NSUInteger indexOfDay2 = [array indexOfObject:day2];
NSUInteger indexOfDay3 = [array indexOfObject:day3];

if (((indexOfDay1 < indexOfDay2 ) && (indexOfDay2 < indexOfDay3)) || 
    ((indexOfDay1 > indexOfDay2 ) && (indexOfDay2 > indexOfDay3))) {
    NSLog(@"YES");
} else {
    NSLog(@"NO");
}



[comps1 release];
[comps2 release];
[comps3 release];
[gregorian release];
NSCalendar*gregorian=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents*comps1=[[NSDateComponents alloc]init];
NSDateComponents*comps2=[[NSDateComponents alloc]init];
NSDateComponents*comps3=[[NSDateComponents alloc]init];
[COMS1设定日:10];
[比较2设定日:12];
[比较3设定日:11];
NSDate*day1=[gregorian dateByAddingComponents:comps1 toDate:[NSDate date]选项:0];
NSDate*day2=[gregorian dateByAddingComponents:comps2 toDate:[NSDate date]选项:0];
NSDate*day3=[gregorian dateByAddingComponents:comps3 toDate:[NSDate date]选项:0];
NSMUTABLEARRY*array=[NSMUTABLEARRY arrayWithObjects:day1,day2,day3,nil];
[数组排序选择器:@选择器(比较:)];
NSUTEGER indexOfDay1=[array indexOfObject:day1];
NSUTEGER indexOfDay2=[array indexOfObject:day2];
NSUTEGER indexOfDay3=[array indexOfObject:day3];
如果((indexOfDay1indexOfDay2)和&(indexOfDay2>indexOfDay3))){
NSLog(@“是”);
}否则{
NSLog(@“否”);
}
[comps1发布];
[comps2发布];
[comps3发布];
[公历释放];

您的问题不清楚,可以详细说明吗?您的问题不清楚,可以详细说明吗?