Ios nsmutablearray中单独的未来日期

Ios nsmutablearray中单独的未来日期,ios,objective-c,nsmutablearray,nsarray,nsdate,Ios,Objective C,Nsmutablearray,Nsarray,Nsdate,我在那个数组中有一个可变数组,我只有过去日期、当前日期和未来日期。我必须将未来的日期和存储区分离到另一个数组中。你能帮我吗 这是我的阵列: uniqueItems( "2015-03-01", "2015-02-28", "2015-02-22", "2015-02-21", "2015-02-20", "2015-02-15", "2015-02-14", "2015-02-13", "2015-02-08", "

我在那个数组中有一个可变数组,我只有过去日期、当前日期和未来日期。我必须将未来的日期和存储区分离到另一个数组中。你能帮我吗

这是我的阵列:

uniqueItems(
    "2015-03-01",
    "2015-02-28",
    "2015-02-22",
    "2015-02-21",
    "2015-02-20",
    "2015-02-15",
    "2015-02-14",
    "2015-02-13",
    "2015-02-08",
    "2015-02-07",
    "2015-02-01",
    "2015-01-31",
    "2015-01-30",
    "2015-01-25",
    "2015-01-24",
    "2015-01-18",
    "2015-01-17",
    "2015-01-16",
    "2015-01-11",
    "2015-01-10",
    "2014-12-07"
我尝试过这种编码,但不起作用

NSDate *currentDate = [NSDate date];
 NSDate* dateAdded=(NSDate*)[uniqueItems objectAtIndex:0];
 double min = [currentDate timeIntervalSinceDate:dateAdded];
int minIndex = 0;
 for (int i = 1; i < [uniqueItems count]; ++i)
{

    double currentmin = [currentDate timeIntervalSinceDate:[uniqueItems objectAtIndex:i]];
    if (currentmin < min) {
        min = currentmin;
        minIndex = i;}}
NSDate*currentDate=[NSDate日期];
NSDate*dateAdded=(NSDate*)[uniqueItems objectAtIndex:0];
double min=[currentDate TimeIntervalsIncestDate:DateAdd];
int minIndex=0;
对于(int i=1;i<[uniqueItems count];++i)
{
double currentmin=[currentDate TimeIntervalsIncestDate:[uniqueItems objectAtIndex:i]];
如果(当前最小值<最小值){
最小值=当前最小值;
minIndex=i;}}

您的数组是字符串格式,不是NSDate对象。可以将字符串转换为nsdate

NSString *dateString = @"2010-02-13";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
然后您可以进行检查。

NSDate*currentDate=[NSDate-date];
NSDate *currentDate = [NSDate date];
NSDate *date;
int offset = 0;
NSMutableArray *futureArray = [[NSMutableArray alloc] init];
for (int i = 1; i < [uniqueItems count]; ++i)
{
    date = [uniqueItems objectAtIndex:i];//TODO: Convert to NSDate
    offset = [currentDate timeIntervalSinceDate:date];
    if (offset < 0) {
    [futureArray addObject:[uniqueItems objectAtIndex:i]];
}
}
NSDate*日期; 整数偏移=0; NSMutableArray*futureArray=[[NSMutableArray alloc]init]; 对于(int i=1;i<[uniqueItems count];++i) { 日期=[uniqueItems objectAtIndex:i];//TODO:转换为NSDate 偏移量=[currentDate-TimeIntervalenceDate:date]; 如果(偏移量<0){ [futureArray addObject:[uniqueItems objectAtIndex:i]]; } }

这是一个想法。对不起,我正在记事本中编码,没有测试。

您可以使用
NSPredicate
块进行编码。您的数组不包含
NSDate
对象,因此我们需要将其转换为
NSDate
,然后与
[NSDate date]
进行比较

NSPredicate *findFutureDates = [NSPredicate predicateWithBlock: ^BOOL(id obj, NSDictionary *bind){
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy-MM-dd"];
    [df setTimeZone:[NSTimeZone systemTimeZone]];
    NSDate *dd = [df dateFromString:(NSString *)obj ];
    return ([[NSDate date] compare:dd] == NSOrderedAscending);
}];

NSArray *arrFutureDates = [yourArray filteredArrayUsingPredicate: findFutureDates];
NSLog(@"%@",arrFutureDates);

如果您通过
取消搜索
将为您提供未来日期,如果您通过
取消搜索
将为您提供过去日期。

您在
唯一项目中的项目
不是
NSDate
。它们似乎是
NSString
。你应该使用
NSDateFormatter
。你能帮我编码吗。