views:

45

answers:

2

Hi

i have got a function with BOOL return value and 2 input (NSDateComponents *) parameters. My trouble is I have two NSDateComponents values and I want to know if the two date fall within the same calendar week. i tried the simplest solutions to solve problem, my idea was the following:

- (BOOL)isFunctionName:(NSDateComponents *)comp1 andParam:(NSDateComponents *)comp2 { return (([comp1 week] == [comp2 week]) && ([comp1 year] == [comp2 year])); }

but it's not correct. what way i can solve it ?


edited

so i have a function which makes datecomponents from dates.

-(NSDateComponents *)dateToDateComponents:(NSDate *)date { unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *dateComponents = [gregorian components:unitFlags fromDate:date]; [gregorian release]; return dateComponents; }

and i call it this way:

if ([self isFunctionName: [self dateToDateComponents:startDate] and Param:[self dateToDateComponents:currentTripDate]]){ }

and during my test it returns YES all of my dates (for example 2010.07.21 - 2010.08.18)

A: 

Did you define your method in your .h file properly?

  • (BOOL)isFunctionName:(NSDateComponents *)comp1 andParam:(NSDateComponents *)comp2;

If not the method might might return something unusual.

What is the output of

NSLog(@"%d", [self isFunctionName:comp1 andParam:comp2]); ?

Boris
of course defined
Victor
A: 

The NSDateComponent class reference states:

Important: An NSDateComponents object is meaningless in itself; you need to know what calendar it is interpreted against, and you need to know whether the values are absolute values of the units, or quantities of the units.

What about comparing NSDates instead?

- (BOOL) weekIsEqual:(NSDate *)date and:(NSDate *)otherDate {
  NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

  NSDateComponents *dateComponents      = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit fromDate:date];
  NSDateComponents *otherDateComponents = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit fromDate:otherDate];

  return [dateComponents week] == [otherDateComponents week] && [dateComponents year] == [otherDateComponents year];
}

edit: In this line:

 unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;

You don't pass NSWeekCalendarUnit, therefore [dateComponent week] returns NSUndefinedDateComponent

edit

Of course, it has to be NSWeekCalendarUnit

Robert
i call my functions as following: if ([self isFunctionName: [self dateToDateComponents:startDate] and Param:[self dateToDateComponents:currentTripDate]]){}
Victor
there is dateToDateComponents function, which is makes DateComponents from datesunsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];NSDateComponents *dateComponents = [gregorian components:unitFlags fromDate:date];[gregorian release];return dateComponents;
Victor
so it was two dates, and i want to handle them.
Victor
ok, wait i edit my question :S
Victor
spotted the bug, see my edit
Robert
ok i see, added NSWeekdayCalendarUnit, and afterward i got EXC_BAD_ACCES after NSLog(@"comp1 week: %@" [comp1 week]) :S
Victor
[comp1 week] returns an NSInteger, if you use e.g. NSLog(@"%@", 34), the 34 will be dereferenced as if it was a pointer which will cause the bad access signal.TryNSLog(@"comp1 week: %i", [comp1 week]);instead
Robert
in the meantime I've tried many solutions, %i was good idea, but comp week value is 2147483647 (upper limit of NSInteger) still don't have result :/
Victor
Sorry, mistake on my part, see the above edit
Robert
You have to pass `NSWeekCalendarUnit` to get the date component that corresponds to the week.
Robert
omg.. thanks ^^ i'm happy lol it's working now.
Victor