在 nmutablearray 中单独的未来日期 [英] separate future date in nsmutablearray

查看:33
本文介绍了在 nmutablearray 中单独的未来日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在该数组中有一个可变数组,我只有过去的日期、当前日期和未来日期.我必须分开未来的日期 &存储到另一个数组中.你能帮我吗.

I am having a mutable array in that array i am having only past date, current date & future date. I have to separate the future date & store into another array. Can you help me.

这是我的数组:

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"

我已经尝试过这种编码,但不起作用.

I have tried this coding, but is not working.

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;}}

推荐答案

你可以使用 NSPredicate 块.您的数组不包含 NSDate 对象,因此我们需要将其转换为 NSDate 然后与 [NSDate date]

you can use NSPredicate block for that. your array does not contain NSDate objects so we need to convert it into NSDate and then compare with [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);

如果你通过了 NSOrderedAscending 会给你未来的日期如果你通过 NSOrderedDescending 会给你过去的日期.

if you pass NSOrderedAscending will give you future dates if you pass NSOrderedDescending will give you past dates.

这篇关于在 nmutablearray 中单独的未来日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆