Amazon.com Widgets Exercise 15.1 years:months:etc. since date - method question
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 22, 2013, 08:00:14 AM
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
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 15
| | | |-+  Exercise 15.1 years:months:etc. since date - method question
Pages: [1]   Go Down
Print
Author Topic: Exercise 15.1 years:months:etc. since date - method question  (Read 1475 times)
Robot-Scott
Jr. Member
**
Posts: 52







« on: July 05, 2009, 10:45:14 AM »

Ok, I'm having a really hard time wrapping my head around dates.  So far, I've only been able to figure out how to set the timezone, create two dates and display them via NSLog; like this:
Code: (Objective-C)
NSTimeZone *myzone = [NSTimeZone systemTimeZone];
NSLog(@"The timezone is %@", [myzone description]);
NSCalendarDate *startDate = [NSCalendarDate dateWithYear:2005 month:5 day:23 hour:13 minute:34 second:45 timeZone:myzone];
NSLog(@"The first date is %@", [startDate description]);
NSCalendarDate *endDate = [NSCalendarDate dateWithYear:2009 month:6 day:7 hour:23 minute:27 second:23 timeZone:myzone];
NSLog(@"The second date is %@", [endDate description]);

What I can't figure out is how to use this method:
Code: (Objective-C)
- (void)years:(NSInteger *)yp months:(NSInteger *)mop days:(NSInteger *)dp hours:(NSInteger *)hp minutes:(NSInteger *)mip seconds:(NSInteger *)sp sinceDate:(NSCalendarDate *)date;

Any hints on plugging the two NSCalendarDates into the preceding method?

Also, how does one get the integer object year from a CalendarDate object (e.g. NSLog(@"%i", [startDate year]) would print "2005")?

Thanks.
« Last Edit: July 05, 2009, 10:47:06 AM by Robot-Scott » Logged

"Think globally, act within local variable scope."
esc
Global Moderator
Full Member
*****
Posts: 230






« Reply #1 on: July 05, 2009, 12:32:53 PM »

The method years:months:days:hours:minutes:seconds:sinceDate: returns values in its parameters.

So you set up two dates like this:
Code: (Objective-C)
NSCalendarDate *today = [NSCalendarDate dateWithYear:2009 month:7 day:5 hour:0 minute:0 second:0 timeZone:[NSTimeZone timeZoneWithAbbreviation:@"PST"]];

NSCalendarDate *yesterday = [NSCalendarDate dateWithYear:2009 month:7 day:4 hour:0 minute:0 second:0 timeZone:[NSTimeZone timeZoneWithAbbreviation:@"PST"]]

Then you use years:months:days:hours:minutes:seconds:sinceDate: to find out the difference in years/months/days/hours/minutes/seconds between the receiver and a given date, e.g.
Code: (Objective-C)
NSInteger secs;
[today years:NULL months:NULL days:NULL hours:NULL minutes:NULL seconds:&secs sinceDate:yesterday];
NSLog(@"number of seconds since yesterday=%i", secs);
Logged
Robot-Scott
Jr. Member
**
Posts: 52







« Reply #2 on: July 05, 2009, 01:04:39 PM »

Thank you, it worked!

Just a follow-up question: Why do you have to use the '&' character?
Logged

"Think globally, act within local variable scope."
esc
Global Moderator
Full Member
*****
Posts: 230






« Reply #3 on: July 05, 2009, 01:41:44 PM »

Thank you, it worked!

Just a follow-up question: Why do you have to use the '&' character?

I figure you might ask that Smiley  Because you need to pass in the address of the variable (i.e. pass by reference) so that the method itself can store/change the value of the memory referenced by the variable that you passed in.  A hint you need to pass by reference is in the method parameters:
- (void)years:(NSInteger *)yearsPointer months:(NSInteger *)monthsPointer...

Note that parameter asked for (NSInteger *), not asking for a NSInteger, AND explanation of the parameters "Upon return":
yearsPointer
Upon return, contains the number of years between the receiver and date. Pass NULL to ignore this component.

monthsPointer
Upon return, contains the number of months between the receiver and date. Pass NULL to ignore this component.
...

Check out P. 283 for explanation of the & operator.
Logged
Robot-Scott
Jr. Member
**
Posts: 52







« Reply #4 on: July 05, 2009, 11:02:33 PM »

Thanks!
Logged

"Think globally, act within local variable scope."
webwrx
Newbie
*
Posts: 41






« Reply #5 on: August 21, 2009, 03:20:13 PM »

Here's my answer to 15.1

Code: (Objective-C)
#import <Foundation/Foundation.h>


@interface NSCalendarDate (ElapsedDays)
-(NSInteger) numberOfElapsedDays: (NSCalendarDate *) theDate;
@end


@implementation NSCalendarDate (ElapsedDays)

-(NSInteger) numberOfElapsedDays: (NSCalendarDate *) theDate
{
NSInteger days;

[self years:NULL months:NULL days:&days  hours:NULL
   minutes:NULL seconds:NULL sinceDate:theDate];

return days;
}

@end


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

        // I used different time zones as a point of interest.
// e.g. if you make them both PST you'll get a different result

NSCalendarDate *dateOfBirth = [NSCalendarDate dateWithYear:1938 month:3 day:25 hour:23 minute:30 second:0 timeZone:[NSTimeZone timeZoneWithAbbreviation:@"EST"]];

NSCalendarDate *dateOfDeath = [NSCalendarDate dateWithYear:2009 month:8 day:22 hour:23 minute:5 second:0 timeZone:[NSTimeZone timeZoneWithAbbreviation:@"PST"]];

NSInteger daysSince;

daysSince = [dateOfDeath numberOfElapsedDays: dateOfBirth];
NSLog (@"The person lived for %li days.", daysSince);

[pool drain];
    return 0;
}

Logged
Pages: [1]   Go Up
Print
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.