Welcome, Guest. Please login or register.
Did you miss your activation email?
July 27, 2016, 05:59:10 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 15
| | | |-+ Exercise 15.1 (all in main), not working? Help.
Pages: [1] Go Down
Author Topic: Exercise 15.1 (all in main), not working? Help. (Read 762 times)
Posts: 28

on: February 25, 2012, 04:37:43 PM

I've got a bug here apparently, and I can't find it.  The output of this program is 0, when it should be 7.  What am I doing wrong?  The components variable is null after initialization for some reason.  Therefore nothing is returned from [components day].

(I realize that we're supposed to make a new elapsedDays method, but for the sake of simplicity I need to figure out what's wrong here first).

Code: (Objective-C)
NSDate *now = [[NSDate alloc] init];
        NSTimeInterval secondsPerWeek = 24 * 60 * 60 * 7;
        NSDate *oneweekfromnow = [[NSDate alloc] initWithTimeIntervalSinceNow:secondsPerWeek];
        NSCalendar *mycalendar = [[NSCalendar alloc] init];
        NSDateComponents *components = [mycalendar components:NSDayCalendarUnit fromDate:now toDate:oneweekfromnow options:0];
        NSInteger days = 0;
        days = [components day];
        NSLog(@"The elapsed days are %li", (long) days);
Last Edit: February 25, 2012, 05:06:36 PM by Hesadanza Logged
Posts: 28

Reply #1 on: March 04, 2012, 11:39:01 AM

I think I figured out what I did wrong here.  I initialized the mycalendar variable wrong.  You can't just alloc-init.  Something like [NSCalendar currentCalendar] is required to correctly initialize a calendar, so it knows what type of calendar you want to use.
Pages: [1] Go Up
Jump to:

Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.