Amazon.com Widgets Chapter 15 Exercise 1
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 05:25:04 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
|-+  Programming in Objective-C, 4th edition
| |-+  Exercises
| | |-+  Chapter 15
| | | |-+  Chapter 15 Exercise 1
Pages: [1]   Go Down
Print
Author Topic: Chapter 15 Exercise 1  (Read 649 times)
clouded
Full Member
***
Posts: 123






« on: June 13, 2012, 09:16:40 AM »

Here's my take on this exercise:

Code: (Objective-C)
//  Chapter 15 Exercise 1
// Look up the NSDate class in your documentation. Then add a new category to
// NSDate called ElapsedDays. In that new category, add a method based on the
// following method declaration:
//      -(unsigned long) elapsedDays: (NSDate *) theDate;
// Have the new method return the number of elapsed days between the receiver and
// the argument to the method. Write a test program to test your new method.

//  main.m

#import <Foundation/Foundation.h>
#import "NSDate+ElapsedDays.h"

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        unsigned long elapsedTime;
        
        NSDate *progDate = [NSDate dateWithString: @"2012-06-12 10:00:00 -0500"];
        NSDate *todaysDate = [NSDate date]; //current @ last modified time
        
        elapsedTime = [todaysDate elapsedDays: progDate];
        
        NSLog(@"%lu days since [NSDate dateWithString: @\"2012-06-12 10:00:00 -0500\"]", elapsedTime);
        
    }
    return 0;
}
Code: (Objective-C)
//  NSDate+ElapsedDays.h

#import <Foundation/Foundation.h>

@interface NSDate (ElapsedDays)
-(unsigned long) elapsedDays: (NSDate *) theDate;
@end
Code: (Objective-C)
//  NSDate+ElapsedDays.m

#import "NSDate+ElapsedDays.h"

@implementation NSDate (ElapsedDays)
-(unsigned long) elapsedDays: (NSDate *) theDate
{
    unsigned long elapsed;
    
    elapsed = [self timeIntervalSinceDate: theDate];
    
    return (unsigned long) elapsed/86400; //day = 60sec * 60min * 24hrs = 86400
}
@end

Output:

0 days since [NSDate dateWithString: @"2012-06-12 10:00:00 -0500"]
Logged
Justwarrior
Newbie
*
Posts: 25


Email




« Reply #1 on: July 09, 2012, 02:34:20 AM »

Hi, I think your code doesn't work...

I wrote my own code and it was 90% similar to your and didn't work.

I always get 0...

And I tried another code with NSDateFormatter, but there I got result 212401089etc....

Can you help me ?

My code is here:
Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface NSDate (ElapsedDays)

-(unsigned long)elapsedDays:(NSDate *) theDate;

@end
@implementation NSDate (ElapsedDays)

-(unsigned long)elapsedDays:(NSDate *) theDate
{
    return (unsigned long) [self timeIntervalSinceDate: theDate]/86400;
}

@end

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        NSDate *TodayD = [NSDate dateWithString:@"2012:12:21 00:01 -0500"];
        NSDate *TodayA = [NSDate dateWithString:@"2012:12:20 00:01 -0500"];
        NSLog(@"%lu", [TodayD elapsedDays: TodayA]);
    }
   
    return 0; }
« Last Edit: July 09, 2012, 02:37:30 AM by Justwarrior » Logged
clouded
Full Member
***
Posts: 123






« Reply #2 on: July 09, 2012, 05:18:48 AM »

Change one of your dates... it's only showing a minute difference. This exercise works in days, so a minute difference would only show 0 in the result
Logged
Justwarrior
Newbie
*
Posts: 25


Email




« Reply #3 on: July 09, 2012, 05:50:20 AM »

I changed the date. The result is the same - 0.

Can't understand why it doesn't work
Logged
siavashApp
Newbie
*
Posts: 17






« Reply #4 on: July 09, 2012, 08:41:44 AM »

Here is your problem Justwarrior: you forgot to insert the hour.
you just typed 00:01.

         NSDate *TodayD = [NSDate dateWithString:@"2012:12:21 00:00:01 -0500"]; 
         NSDate *TodayA = [NSDate dateWithString:@"2012:12:20 00:00:01 -0500"]; 
         NSLog(@"%lu", [TodayD elapseDays:TodayA]);
and the strange number that you get at first are the result of the (unsigned long) that we had declared in implementation.

Cheers
Siavash
Logged
mo7ionsickness
Jr. Member
**
Posts: 50






« Reply #5 on: September 09, 2012, 12:32:59 PM »

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

@interface NSDate (ElapsedDays)

-(unsigned long) elapsedDays: (NSDate *) theDate;

@end
Code: (Objective-C)
#import "NSDate+ElapsedDays.h"

@implementation NSDate (ElapsedDays)

-(unsigned long) elapsedDays:(NSDate *)theDate
{
    return [self timeIntervalSinceDate:theDate] / 86400;
}

@end
Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "NSDate+ElapsedDays.h"

int main(int argc, const char * argv[])
{
    
    @autoreleasepool {
        
        NSDateComponents *dayMonthYear = [[NSDateComponents alloc]init];
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        
        [dayMonthYear setDay:16];
        [dayMonthYear setMonth:11];
        [dayMonthYear setYear:2008];

        NSDate *date1 = [calendar dateFromComponents:dayMonthYear];
        
        [dayMonthYear setDay:9];
        [dayMonthYear setMonth:9];
        [dayMonthYear setYear:2012];
        
        NSDate *date2 = [calendar dateFromComponents:dayMonthYear];
        
        printf("Elapsed days from %s - %s is %lu days",
              [date1.description UTF8String], [date2.description UTF8String],
               [date2 elapsedDays:date1]);
        
    }
    return 0;
}
Elapsed days from 2008-11-16 00:00:00 +0000 to 2012-09-08 23:00:00 +0000 is 1392 days
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.