0

My goals:

  • Determine current day of the week (Sun = 1, Mon = 2, etc.)
    so that i can (if dayInt == 2){ ...

  • Then compare current time to set time forming a nested if with the statement above
    (if curTime > 9:00am && curTime < 11:00am){...

I've researched this topic and found the below code is a somewhat popular way of doing the first objective. I found the below code at
iPhone - how may I check if a date is Monday?
However, for some reason it causes a breakpoint in my code. It tells me to replace NSWeekdayCalendarUnit with NSCalendarUnitWeekday (which I do) and have dayInt be a long(which I have tried), but these still do not work.
It builds but then causes a breakpoint. If dayInt is an int it says the value is 0, but if it is long it comes out as 256. I don't really see a need for it to be long though. Any ideas?

NSDate* curDate = [NSDate date];
int dayInt = [[[NSCalendar currentCalendar] components: NSWeekdayCalendarUnit fromDate: curDate] weekday];
Community
  • 1
  • 1
DSmith
  • 159
  • 1
  • 1
  • 13
  • 1
    You say "...these do not work." That is profoundly unhelpful. What happens? Does it crash? Does it fail to compile? If it crashes, what is the exact error message you get? I suggest breaking your code into multiple lines that separate out the different steps, and then walking through it in the debugger. – Duncan C Aug 06 '16 at 14:20
  • Oh yes, and edit your question to show the code that compiles, not your original code that you said you've already changed. – Duncan C Aug 06 '16 at 14:21
  • 1
    "it causes a breakpoint in my code" If you really mean "breakpoint", that's [not an error, it's something that _you_ set up](http://stackoverflow.com/questions/10016890/thread-1-stopped-at-breakpoint-error-when-initializing-an-nsurl-object/10016939#10016939). – jscs Aug 06 '16 at 15:36
  • There is no price for writing code in the smallest possible number of lines. But it makes debugging so much harder. Better to write NSCalendar* calendar = ...; NSDateComponents* components = ...; NSInteger weekDay = ...; because then you have a chance to check with the debugger what's going wrong. And don't call a variable containing the week day "dayInt". – gnasher729 Aug 06 '16 at 15:47

1 Answers1

0

Your code works just fine for me once I apply the changes you describe:

NSDate* curDate = [NSDate date];
NSInteger dayInt = [[[NSCalendar currentCalendar] components: NSCalendarUnitWeekday fromDate: curDate] weekday];
NSLog(@"Today's day of the week = %ld", dayInt);

Here is what I mean by breaking it into smaller steps for debugging:

NSDate* curDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components;
components = [calendar components: NSWeekdayCalendarUnit fromDate: curDate];
NSInteger dayInt = 0;
dayInt = [components weekday];
NSLog(@"Today's day of the week = %ld", dayInt);

When you make each line of your code contain at most one method call then it's much easier to step through it in the debugger and see what's happening. The example above is a little extreme to make a point, but in general I prefer to use temporary variables and series of simpler statement rather than a big complex statement. It makes it easier to read and debug, and the compiler optimizes away the temporary variables so there's rarely ANY difference in performance.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • What was the problem with your code? Posting the specifics of your solution could help somebody else. – Duncan C Aug 06 '16 at 15:36
  • Honestly, still unsure as to what was wrong. But, breaking into sections like you suggested worked. I appreciate the help! So, if I've used your outline to continue with the code, but have a question about the second part of the goals above, do I edit the post or create a new one? Sorry, not sure what the protocol is. – DSmith Aug 06 '16 at 15:51