Does NSDateComponents work on the simulator?

Discussion in 'iOS Programming' started by ataylor2009, Mar 19, 2010.

  1. ataylor2009 macrumors member

    Joined:
    Jan 27, 2009
    #1
    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?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    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?
     
  3. ataylor2009 thread starter macrumors member

    Joined:
    Jan 27, 2009
    #3
    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.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Why not try pasting the actual code since the above won't even compile?
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    Run it in the debugger and see which exact line is causing the crash...
     
  6. ataylor2009 thread starter macrumors member

    Joined:
    Jan 27, 2009
    #6
    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
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    The problem lies here:
    Code:
    NSInteger day = [comps day];
    NSLog(@"Day = %@", day);
    "%@" is not how you output NSInteger.
     
  8. ataylor2009 thread starter macrumors member

    Joined:
    Jan 27, 2009
    #8
    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.
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    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.
     
  10. ataylor2009 thread starter macrumors member

    Joined:
    Jan 27, 2009
    #10
    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?
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    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?
     
  12. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #12
    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
     
  13. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #13
    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.
     
  14. RashiMahajan macrumors newbie

    Joined:
    Apr 17, 2009
  15. ataylor2009 thread starter macrumors member

    Joined:
    Jan 27, 2009
    #15
    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.
     

Share This Page