I fixed this issue by replacing the 'if' line with:
if ( [components hour] <= 5 && [components hour] >= 9){
and removing the NSUInteger
line.
Question
I'm having issue with the code below. What's the issue with it? I have tried defining the NSUInteger
as an int
, but it still didn't work.
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSHourCalendarUnit fromDate:nowNow];
NSLog(@"%ld",[components hour]);
NSUInteger *timeHour = [components hour];
if ( timeHour < 5 && timeHour > 9){}
This piece of code is supposed to detect wether it's between 5, and 9 in the morning.
La solution 2
I fixed this issue by replacing the 'if' line with:
if ( [components hour] <= 5 && [components hour] >= 9){
and removing the NSUInteger
line.
Autres conseils
Anything that returns true for timeHour < 5
automatically will return false for timeHour > 9
. You're anding these two conditions, which can never both be true at the same time, so you'll always return a false.
Also, without anything in the if
block, it's pretty impossible to tell whether or not it's executing.
You might be looking for something more like this:
if (timeHour >= 5 && timeHour <= 9) {
// stuff
}
timeHour >= 5
returns true for anything where timeHour
is greater than or equal to 5.
timeHour <= 9
returns true for anything where timeHour
is less than or equal to 9.
a && b
returns true only when both a
and b
are BOTH true.
EDIT: ALSO... NSUInteger *timeHour = [components hour];
There's a problem in this line I just caught. You're defining timeHour
as an NSUInteger
POINTER, rather than an NSUInteger
. Get rid of the *
and try again.