【问题标题】:Error when getting array of dates with current date from array of dates从日期数组中获取当前日期的日期数组时出错
【发布时间】:2013-08-12 09:50:46
【问题描述】:

我有一个带有 NSDate 对象的 NSMutableArray "NewDate"

(
"2013-08-11 20:26:11 +0000",
"2013-08-11 20:26:11 +0000",
"2013-08-12 06:22:38 +0000",
"2013-08-12 07:24:14 +0000",
"2013-08-12 08:04:05 +0000"
)

我想用今天日期(8 月 12 日)的对象从这个数组中创建一个新数组

我的代码:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
[components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *firstDate = [calendar dateFromComponents:components];
[components setMonth:0];
[components setDay:1];
[components setYear:0];
NSDate *secondDate = [calendar dateByAddingComponents:components toDate:firstDate options:0];

NSPredicate *firstPredicate = [NSPredicate predicateWithFormat:@"startDate > %@", firstDate];
NSPredicate *secondPredicate = [NSPredicate predicateWithFormat:@"startDate < %@", secondDate];

NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:firstPredicate, secondPredicate, nil]];

NSMutableArray *results = [newDate filteredArrayUsingPredicate:predicate];
NSLog(@"%@",result);

但是在创建“结果”时它给了我错误:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDate 0x72a0ee0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key startDate.'

我做错了什么? 感谢您的帮助。

【问题讨论】:

  • 好吧,对于初学者来说,您正在编造一个名为“startDate”的完全虚构的属性。你到底想用你的第一个和第二个谓词做什么?

标签: ios nsarray nsdate nspredicate


【解决方案1】:

试试下面的代码。

array={"2013-08-11 20:26:11 +0000", "2013-08-11 20:26:11 +0000", "2013-08-12 06:22:38 +0000", "2013-08-12 07:24:14 +0000", "2013-08-12 08:04:05 +0000"};/////示例。

NSMutableArray *newArray=[[NSMutableArray alloc]init];
NSDate *today=[NSDate date];

NSComparisonResult result;
//has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending

for(int i=0;i<array.count;i++)
{
result = [today compare:[array objectAtIndex:i]];


if(result==NSOrderedDescending)//////If matches with today's date.
{
   [newArray addObject:[array objectAtIndex:i]];
}
}

【讨论】:

    【解决方案2】:

    试试这个..

    - (NSComparisonResult)compareDateOnly:(NSDate *)otherDate {
        NSUInteger dateFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit;
        NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
        NSDateComponents *selfComponents = [gregorianCalendar components:dateFlags fromDate:self];
        NSDate *selfDateOnly = [gregorianCalendar dateFromComponents:selfComponents];
    
        NSDateComponents *otherCompents = [gregorianCalendar components:dateFlags fromDate:otherDate];
        NSDate *otherDateOnly = [gregorianCalendar dateFromComponents:otherCompents];
        return [selfDateOnly compare:otherDateOnly];
    }
    

    【讨论】:

    • 我不知道你是否问过这个问题,但如果你只想比较 NSDate 的日期组件,你必须使用 NSCalendar 和 NSDateComponents 来删除时间组件。像这样的东西应该作为 NSDate 的一个类别:
    猜你喜欢
    • 1970-01-01
    • 2019-05-21
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 2023-02-08
    • 2019-02-23
    相关资源
    最近更新 更多