Amazon.com Widgets Chapter 15, Exercise 1
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 24, 2014, 10:40:07 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
| |-+ Part 3
| | |-+ Assignments
| | | |-+ Chapter 15, Exercise 1
Pages: [1] Go Down
Print
Author Topic: Chapter 15, Exercise 1 (Read 2381 times)
skochan
Administrator
Hero Member
*****
Posts: 3114







on: August 19, 2011, 11:18:51 AM

Post your solutions by replying to this topic.

Cheers,

Steve
Logged
Bertus
Newbie
*
Posts: 20


'Audaces fortuna iuvat'


WWW Email




Reply #1 on: August 24, 2011, 06:29:44 AM

Got it!

Code: (Objective-C)

//
//  main.m
//  Excerise15_1
//
//  Created by Bertus ten Hoor on 24-08-11.
//  Copyright 2011 Skooter jeans & fashion bv. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSDate (ElapsedDays)

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

@end

@implementation NSDate (ElapsedDays)
    

-(unsigned long) elapsedDays: (NSDate *) theDate
{
    unsigned long result;
    
    result = [self timeIntervalSinceDate:theDate];
    
    return result/86400;  //convert seconds to days

}

@end

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

    NSDate *toDay = [NSDate date];     //sets toDay to today's date
    NSDate *dateToCompare = [NSDate dateWithString:@"2011-08-01 00:01:00 +0000"];     //any given date to compare to

    NSLog(@"The difference in days between %@ and %@ is %lu",toDay, dateToCompare, [toDay elapsedDays:dateToCompare]);

    [pool drain];
    return 0;
}
Last Edit: August 24, 2011, 07:20:27 AM by Bertus Logged
Kevin D
Newbie
*
Posts: 5


Email




Reply #2 on: August 26, 2011, 12:17:37 PM

Homework

Code: (Objective-C)
//
//  NSDate.h
//  ch15ex1
//
//  Created by Kevin on 8/26/11.
//  Copyright 2011 Shoebox Apps, Inc. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface NSDate (ElapsedDays)

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

@end

Code: (Objective-C)
//
//  NSDate.m
//  ch15ex1
//
//  Created by Kevin on 8/26/11.
//  Copyright 2011 Shoebox Apps, Inc. All rights reserved.
//

#import "NSDate.h"


@implementation NSDate (ElapsedDays)

- (unsigned long) elaspsedDays: (NSDate *) theDate
{
unsigned long timeSince;
timeSince = [self timeIntervalSinceDate:theDate];  // gives number in seconds
int secsInDay = 86400; // The number of seconds in a day
return timeSince/secsInDay;
}

@end

Code: (Objective-C)
//
//  main.m
//  ch15ex1
//
//  Created by Kevin on 8/26/11.
//  Copyright 2011 Shoebox Apps, Inc. All rights reserved.
//

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

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

NSDate *checkThisDate = [NSDate dateWithString:@"1969-01-22 05:00:00 +0000"];  // my brithday
unsigned long numDays = [[NSDate date] elaspsedDays:checkThisDate];  //get time since 'date'

// Setup formated date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

// Setup comma formated number
NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
[numFormatter setNumberStyle:NSNumberFormatterDecimalStyle];

// Output
NSLog(@"It's been %@ days since %@.",
  [numFormatter stringFromNumber:[NSNumber numberWithUnsignedLong:numDays]],
  [dateFormatter stringFromDate:checkThisDate]);

[pool drain];
    return 0;
}

Output:
It's been 15,556 days since January 22, 1969.

Logged
wjackman
Newbie
*
Posts: 12


WWW




Reply #3 on: September 02, 2011, 10:50:53 AM

Hi - Here's my solution. I banged my head on this one for a while, and finally had to look at other peoples' solutions to get the answer - thanks to Bertus and Kevin D. I don't think I would have figured out the date format without help. It still has a problem I couldn't figure out, where I get EXC BAD ACCESS at the [pool drain] line? Perhaps someone could tell me why?


#import <Foundation/Foundation.h>


@interface NSDate (ElapsedDays)

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

@end



#import "ElapsedDays.h"


@implementation NSDate (ElapsedDays)

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

{
    unsigned long res; //set variable to hold result
   
    res = [self timeIntervalSinceDate: theDate]; //result = time between self and test date
    return (res/86400);                     //convert to days
}

@end



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

int main (int argc, const char * argv[])
{
   
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
    NSDate * testDate = [[NSDate alloc] init]; //date to be tested
    NSDate * today = [NSDate date];             //today's date
    unsigned long answer;
   
    testDate = [NSDate dateWithString: @"1958-08-26 00:01:00 +0000"]; //set up date to be tested
   
    answer = [today elapsedDays: testDate];     //elapsedDays method
   
    NSLog (@"Elapsed days = %lu", answer);
   
    [testDate release];
    [today release];
   
    [pool drain];
    return 0;
}


It's been 19365 days since my birthday, a little depressing if you dwell on it too much.
Logged
dpieretti
Newbie
*
Posts: 11


Email




Reply #4 on: September 02, 2011, 05:19:01 PM


Here is another solution - this is a good exercise because it requires you to "get your hands dirty" with the NSDate documentation.  I also tip my hat to the solutions above.  Nice job!

ElapsedDays.h
Code: (Objective-C)
//
//  ElapsedDays.h
//  DateStuff
//
//  Created by Dennis Pieretti on 9/1/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

// Add new NSDate category ElapsedDays
@interface NSDate (ElapsedDays)

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

@end

ElapsedDays.m

Code: (Objective-C)
//
//  ElapsedDays.m
//  DateStuff
//
//  Created by Dennis Pieretti on 9/1/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ElapsedDays.h"


@implementation NSDate (ElapsedDays)

-(unsigned long) numberOfElapsedDays: (NSDate *) theDate
{
    unsigned long theTimeInSecondsElapsed = [self timeIntervalSinceDate:theDate];
    return theTimeInSecondsElapsed / (24 * 60 * 60);
}

@end

main.m
Code: (Objective-C)
//
//  main.m
//  DateStuff
//
//  Created by Dennis Pieretti on 9/1/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
//  Solution to Ch15 #01

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

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

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
    // set dates to find days between dates  "yyyy-mm-dd hh:mm:ss +GMToffset"
    NSDate *dateOne = [NSDate dateWithString:@"2011-09-09 12:00:00 +0000"];
    NSDate *dateTwo = [NSDate dateWithString:@"2011-09-11 12:00:00 +0000"];
   
    // select a format style for date output from: ShortStyle, MediumStyle, LongStyle, FullStyle
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateStyle:(NSDateFormatterMediumStyle)];
   
    // display the computed days between dates
    NSLog(@"\nThe number of days elapsed from %@ to %@ is %lu",
          [dateFormat stringFromDate: dateOne],
          [dateFormat stringFromDate: dateTwo],
          [dateTwo numberOfElapsedDays: dateOne]);
   
    dateOne = [NSDate dateWithString:@"2011-02-28 12:00:00 +0000"];   
    dateTwo = [NSDate dateWithString:@"2011-03-01 12:00:00 +0000"];
   
    // display the computed days between dates
    NSLog(@"\nThe number of days elapsed from %@ to %@ is %lu", 
          [dateFormat stringFromDate: dateOne],
          [dateFormat stringFromDate: dateTwo],
          [dateTwo numberOfElapsedDays: dateOne]);
   
    // test leap year
    dateOne = [NSDate dateWithString:@"2012-02-28 12:00:00 +0000"];   
    dateTwo = [NSDate dateWithString:@"2012-03-01 12:00:00 +0000"];
   
    // display the computed days between dates
    NSLog(@"\nThe number of days elapsed from %@ to %@ is %lu",
          [dateFormat stringFromDate: dateOne],
          [dateFormat stringFromDate: dateTwo],
          [dateTwo numberOfElapsedDays: dateOne]);
   
    // new year
    dateOne = [NSDate dateWithString:@"2012-12-31 12:00:00 +0000"];   
    dateTwo = [NSDate dateWithString:@"2013-01-01 12:00:00 +0000"];
   
    // display the computed days between dates
    NSLog(@"\nThe number of days elapsed from %@ to %@ is %lu", 
          [dateFormat stringFromDate: dateOne],
          [dateFormat stringFromDate: dateTwo],
          [dateTwo numberOfElapsedDays: dateOne]);
   
    // one year
    dateOne = [NSDate dateWithString:@"2012-01-01 12:00:00 +0000"];   
    dateTwo = [NSDate dateWithString:@"2012-12-31 12:00:00 +0000"];
   
    // display the computed days between dates
    NSLog(@"\nThe number of days elapsed from %@ to %@ is %lu", 
          [dateFormat stringFromDate: dateOne],
          [dateFormat stringFromDate: dateTwo],
          [dateTwo numberOfElapsedDays: dateOne]);
   
    [pool drain];
    return 0;
}


SAMPLE OUTPUT

Code: (Objective-C)
2011-09-02 19:57:25.842 DateStuff[280:903] 
The number of days elapsed from Sep 9, 2011 to Sep 11, 2011 is 2
2011-09-02 19:57:25.844 DateStuff[280:903]
The number of days elapsed from Feb 28, 2011 to Mar 1, 2011 is 1
2011-09-02 19:57:25.844 DateStuff[280:903]
The number of days elapsed from Feb 28, 2012 to Mar 1, 2012 is 2
2011-09-02 19:57:25.846 DateStuff[280:903]
The number of days elapsed from Dec 31, 2012 to Jan 1, 2013 is 1
2011-09-02 19:57:25.846 DateStuff[280:903]
The number of days elapsed from Jan 1, 2012 to Dec 31, 2012 is 365
Program ended with exit code: 0
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #5 on: September 05, 2011, 02:08:40 PM


Hi, testdate doesn't need to be alloc'ed, as it's created and returned to you by the dateWithString: method (and you lose the reference to original alloc'ed object as a result, which would produce a memory leak).  In fact you shouldn't be releasing testate or today, as they're both autoreleased objects created by the NSDate methods.


Cheers,

Steve
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.