检查自定义对象数组是否包含具有特定日期的对象 [英] Check if an array of custom objects contains an object with a certain date

查看:64
本文介绍了检查自定义对象数组是否包含具有特定日期的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组事件对象.对象有几个属性.属性之一是 NSDate eve_date.

I have an array of event objects. The object has several attributes. One of the attributes is an NSDate eve_date.

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

Now I want to check if that array of objects contains a certain NSDate d

我正在做以下事情

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

但这行不通.有人可以帮我吗?

But this is not working. Can anyone help me ?

亲切的问候

编辑

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

Okay let me be more clear. I'm making a calendar app. I fetched all the events inside the particular month. What I need to do now is to place a marker on my calendar on the correct date. Therefore I have this in a function.

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 次(每月休息的天数).(这可能不是最佳实践解决方案???)

But now I'm looping 31 times (amount of days off the month) through my array of events. (This is probably not the best practice solution ???)

我也觉得问题是日期的时间不一样.例如:

I also think that the problem is that the time of the date is not the same. For example:

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

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

So I probably should use an NSDateformatter to only get the date itself without the time ?

我说得对吗?

推荐答案

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

I'm not very well versed with KVC, but if the solution doesn't need to use KVC, you can just iterate:

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

这篇关于检查自定义对象数组是否包含具有特定日期的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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