【问题标题】:Check if an array of custom objects contains an object with a certain date检查自定义对象数组是否包含具有特定日期的对象
【发布时间】:2013-08-20 10:30:41
【问题描述】:

我有一个event objects 数组。该对象有几个属性。其中一个属性是NSDate eve_date

现在我想检查该对象数组是否包含某个NSDate d

我正在做以下事情

if([[matches valueForKey:@"eve_date"] containsObject:d]){
        NSLog(@"contains object");
   }else{
    NSLog(@"does not contains object");
   }

但这不起作用。谁能帮帮我?

亲切的问候

编辑

好的,让我说得更清楚。我正在制作一个日历应用程序。我获取了特定月份内的所有事件。我现在需要做的是在我的日历上标记正确的日期。因此我在一个函数中有这个。

NSLog(@"Delegate Range: %@ %@ %d",start,end,[start daysBetweenDate:end]);

    self.dataArray = [NSMutableArray array];
    self.dataDictionary = [NSMutableDictionary dictionary];

    NSDate *d = start;
    while(YES){
        for (Event *event in matches) {
            if([event.eve_date isEqualToDate:d]){
                //  (self.dataDictionary)[d] = save event title in here
                [self.dataArray addObject:@YES]; //place marker on date 'd'


            }else{
                [self.dataArray addObject:@NO]; // don't place marker
            }
        }


        NSDateComponents *info = [d dateComponentsWithTimeZone:calendar.timeZone];
        info.day++;
        d = [NSDate dateWithDateComponents:info];
        if([d compare:end]==NSOrderedDescending) break;
    }

但现在我在我的一系列事件中循环了 31 次(一个月的休息天数)。 (这可能不是最佳实践解决方案???)

我也认为问题在于日期的时间不一样。例如:

eve_date --> 2013-08-13 12:00
d --> 2013-08-13 15:00

所以我可能应该使用 NSDateformatter 只获取日期本身而不获取时间?

我说的对吗?

【问题讨论】:

  • 您检查(并记录)[matches valueForKey:@"eve_date"] 的内容了吗?
  • 解决方案一定要用KVC吗?
  • 我猜你可以通过 NSPredicate 来实现。
  • 究竟是什么不起作用?您是否已转储 [matches valueForKey:@"eve_date"] 并检查它是否包含等于 d 的日期?
  • 我已经编辑了我的问题

标签: iphone ios objective-c nsdate nsobject


【解决方案1】:

我对KVC不是很精通,但是如果解决方案不需要使用KVC,你可以迭代:

NSDate *dateToCompare = ...;
BOOL containsObject = NO;
for (MyEvent *e in matches)
{
    if ([e.eve_date isEqualToDate:dateToCompare]) 
    { 
        containsObject = YES;
        break;
    }
}

if (containsObject) NSLog(@"Contains Object");
else NSLog(@"Doesn't contain object");

我玩过 KVC 并尝试了一个解决方案。你只缺少valueForKeyPath 而不是valueForKey

if ([[matches valueForKeyPath:@"eve_date"] containsObject:d])
{
    NSLog(@"Contains object");
}
else
{
    NSLog(@"Does not contain object");
}

【讨论】:

  • 非常简单直接的解决方案。 +1
  • 终于成功了!你的回答对我帮助最大!谢谢!!
【解决方案2】:

NSDate 是一个绝对时间点。要检查日期是否在给定的, 您必须将其与“一天的开始”和“第二天的开始”进行比较。

下面的(伪)代码应该演示这个想法:

NSDate *start, *end; // your given range

NSDate *currentDay = "beginning of" start;
// while currentDay < end:
while ([currentDay compare:end] == NSOrderedAscending) {
    NSDate *nextDay = currentDay "plus one day";
    for (Event *event in matches) {
        // if currentDay <= event.eve_date < nextDay:
        if ([event.eve_date compare:currentDay] != NSOrderedAscending
            && [event.eve_date compare:nextDay] == NSOrderedAscending) {
            // ...
        }
    }
    currentDay = nextDay;
}

“一天的开始”可以如下计算:

NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *aDate = ...;
NSDate *beginningOfDay;
[cal rangeOfUnit:NSDayCalendarUnit startDate:&beginningOfDay interval:NULL forDate:aDate];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    相关资源
    最近更新 更多