Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ataylor2009

macrumors member
Original poster
Jan 27, 2009
78
0
I'm trying to get the current time and the current day of the week (i.e. Monday, Tuesday, etc.). I'm using the code straight from the Apple Documentation:

Code:
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];

When I insert this code into my RootViewController's viewDidLoad method along with a couple of NSLog statements to see what's going on, it crashes my previously-working app.

Anybody have any idea what I'm doing wrong here?
 
Anything in the crash logs? Have you tried setting a breakpoint and stepping through? Any other debugging attempts?

P.S. Can you include the NSLog calls?
 
Here's my code:

Code:
NSDate *now = [NSDate date];
NSLog(@"Date = %@", now];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [currentCalendar components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
NSInteger day = [comps day];
NSLog(@"Day = %@", day];
NSInteger weekday = [comps weekday];
NSLog(@"Weekday = %@", weekday];

The only thing in the console when I do a Build & Run is:

Code:
[Session started at 2010-03-19 10:39:27 -500.]
// bunch of GNU gdb legalese
Attaching to process 40210.
2010-03-19 10:40:29.023 gdb-i386-apple-darwin[40212:60f] _CFGetHostUUIDString: unable to determine UUID for host. Error: 35
Program received signal: "EXC_BAD_ACCESS".
(gdb)

If I comment out all the date code above, the app works fine.
 
Here's my code:

Code:
NSDate *now = [NSDate date];
NSLog(@"Date = %@", now];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [currentCalendar components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
NSInteger day = [comps day];
NSLog(@"Day = %@", day];
NSInteger weekday = [comps weekday];
NSLog(@"Weekday = %@", weekday];
Why not try pasting the actual code since the above won't even compile?
 
I put breakpoints at every line of the above code. Here's what I get:

at NSDate *now = [NSDate date]; "now" is "out of scope"
at NSCalendar *currentCalendar... "now" is 2010-03-19 10:45:15 -0500, "currentCalendar" is blank
at NSDateComponents *comps... "now" is unchanged, "currentCalendar" is 0x59153c0
at NSInteger day... "now" and "currentCalendar" are unchanged, "comps" is 0x59098c0, "day" is 1
at NSLog(@"Day =... all are unchanged except "day" is 19
at NSInteger weekday... I get the "EXC_BAD_ACCESS" error
 
Why not try pasting the actual code since the above won't even compile?

Short version is, I'm remote-accessing my Mac at home to view and work on the code, and I am unable to cut & paste between that computer and the one I'm physically working on (and posting here from) at work...just trying to keep the re-typing to a minimum.
 
Short version is, I'm remote-accessing my Mac at home to view and work on the code, and I am unable to cut & paste between that computer and the one I'm physically working on (and posting here from) at work...just trying to keep the re-typing to a minimum.
Understood. But when you provide code that doesn't even compile, it's pretty hard for us to help you troubleshoot it. Please, copy it by-hand exactly or wait until you get home.
 
The problem lies here:
Code:
NSInteger day = [comps day];
NSLog(@"Day = %@", day);
"%@" is not how you output NSInteger.

Sure enough...when I commented out the NSLog calls, the app works fine, and the debugger shows the expected values for the day and weekday. Thanks for the tip.

I'm new to all of this; I thought the whole point of "NSInteger" was to wrap a value in an object wrapper, and it would therefore print using the standard "%@" notation. What did I miss there?
 
I'm new to all of this; I thought the whole point of "NSInteger" was to wrap a value in an object wrapper, and it would therefore print using the standard "%@" notation. What did I miss there?
NSInteger is not a class, it's a typedef. That's why you didn't use the "*" to make it a pointer-reference. Perhaps you're thinking of NSNumber?
 
I'm new to all of this; I thought the whole point of "NSInteger" was to wrap a value in an object wrapper, and it would therefore print using the standard "%@" notation. What did I miss there?

You assumed that just because its name started with "NS" that it was an object. This is wrong.

There are a number of types, such as NSInteger and NSPoint, which are typedefs, and are not Objective-C object pointers. As already mentioned, you can often tell these from object types because the typedefs are not pointers (they don't use *, as in NSString *). This isn't always a sure-fire way to tell though; sometimes an NSPoint* is used, and it means a pointer to an NSPoint structure, not a reference to an NSPoint object. That is, "%@" would be just as wrong for NSPoint* as it is for NSPoint or NSInteger.

You can also spend some time and become familiar with these types:
http://developer.apple.com/Mac/libr...Foundation_DataTypes/Reference/reference.html
 
Short version is, I'm remote-accessing my Mac at home to view and work on the code, and I am unable to cut & paste between that computer and the one I'm physically working on (and posting here from) at work...just trying to keep the re-typing to a minimum.

You can remotely tell your Mac at home to visit pastebin.com. Then copy and the paste the code there. Then post the url. Or you can visit the url yourself using your work computer, and copy and paste the code itself into a post.
 
Got it...problem solved, question answered.

Which is why I come here for help. Next best thing to having a live group of buddies to bounce things off of and ask questions to.

I've said it a bunch, but it bears repeating: Thank you all for the assistance you've provided me; I hope to someday be a contributor of solutions, instead of an asker of stupid questions.

Cheers.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.