问题
I have an array of event objects
. The object has several attributes. One of the attributes is an NSDate eve_date
.
Now I want to check if that array of objects contains a certain NSDate d
I'm doing the following
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 ?
Kind regards
EDIT
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;
}
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
So I probably should use an NSDateformatter to only get the date itself without the time ?
Am I correct ?
回答1:
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");
I've had a play about with KVC and attempted a solution with that too. You were only missing valueForKeyPath
instead of valueForKey
if ([[matches valueForKeyPath:@"eve_date"] containsObject:d])
{
NSLog(@"Contains object");
}
else
{
NSLog(@"Does not contain object");
}
回答2:
NSDate
is an absolute point in time. To check if a date falls on a given day,
you have to compare it with the "beginning of the day" and "beginning of the next day".
The following (pseudo-) code should demonstrate the idea:
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;
}
The "beginning of a day" can be computed as follows:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *aDate = ...;
NSDate *beginningOfDay;
[cal rangeOfUnit:NSDayCalendarUnit startDate:&beginningOfDay interval:NULL forDate:aDate];
来源:https://stackoverflow.com/questions/18332769/check-if-an-array-of-custom-objects-contains-an-object-with-a-certain-date