Challenge Possible Solution


#1

This seemed to work for me …

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        [comps setYear:1963];
        [comps setMonth:4];
        [comps setDay:12];
        
        NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        
        //These are the Dates to Compare
        NSDate *myBirthDay = [g dateFromComponents:comps];
        NSDate *now = [NSDate date]; //Todays Date
        
        double secondsBetween = [now timeIntervalSinceDate:myBirthDay];
        NSLog(@"There are %f seconds between your birthday and today.", secondsBetween);   
        
    }
    return 0;
}

#2

Thanks for sharing Brian - this one had me stumped.


#3

I had several unsuccessful attempts throwing all kinds of errors, then I walked away from the computer for awhile, re-read the suggestion and it just popped to me. I consider myself a JavaScript guru, been coding in it for 10 years, but I find Objective C so foreign; after rereading the chapters and trying all the challenges I think I know so much more than when I started.


#4

I’ve heard one you learn one language, it’s easier to pick up others? Any truth to that? I’ve wanted for a long time to get into software development, but my current job limits the time I can spend learning it.

My high school computer class consisted of word processing and spreadsheets (Wordperfect 5.1 and Lotus), with my teacher freaking out if I ever got to a DOS prompt. I envy the kids today that get to learn this kind of stuff in school.


#5

I’m using Xcode 4.1 as I have several books to go through (including iOS Programming - TBNRG, second edition) and wanted all the examples to work. I only point that out as my code is a little different than the previous one in this thread.

Also, I left all code in from the examples in the chapter.

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

    NSDate *now = [NSDate date];
    NSLog(@"The new date is %@", now);
    
    double seconds = [now timeIntervalSince1970];
    NSLog(@"It has been %f seconds since the start of 1970.", seconds);
    
    NSDate *later = [now dateByAddingTimeInterval:100000];
    NSLog(@"In 100,000 seconds it will be %@", later);
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setYear:1970];
    [comps setMonth:4];
    [comps setDay:16];
    [comps setHour:19];
    [comps setMinute:45];
    [comps setSecond:0];
    
    NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *dateOfBirth = [g dateFromComponents:comps];
    
    double secondsAlive = [now timeIntervalSinceDate:dateOfBirth];
    NSLog(@"Brian has been alive for %f seconds", secondsAlive);

    [pool drain];
    return 0;
}

#6

I’m using Xcode 4.2 and here was my solution to the challenge…

[code]#import <Foundation/Foundation.h>

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

@autoreleasepool {
    
    // Create a date object for my birthday
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setYear:1974];
    [comps setMonth:2];
    [comps setDay:2];
    [comps setHour:16];
    [comps setMinute:0];
    [comps setSecond:0];
    
    NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *dateOfBirth = [g dateFromComponents:comps];
    
    // Make a date for right now
    NSDate *now = [NSDate date];
    
    // Compare the two dates in seconds
    double secondsDifference = [now timeIntervalSinceDate:dateOfBirth];
    
    // Print the difference
    NSLog(@"I was born roughly %f seconds ago!", secondsDifference);
    
}
return 0;

}[/code]


#7

Thanks so much for this book! It’s really working for me & I’ve tried a few others in the past and have given up. Had I started with this ages ago, I’d be an uber programmer by now! The challenges are what really help me learn.

I like coming here after I figure one out & see how others did it too.


#8

Here is another alternative that uses the methods taught so far (for the sake of mental exercise):

// Since we already have seconds since 1970 (from earlier exercise in the chapter), 
// double seconds = [now timeIntervalSince1970];
// deduct from that the dateOfBirth timeIntervalSince1970 (provided you were born after 1970)

double secondsDateOfBirth = [dateOfBirth timeIntervalSince1970];
double myLifeInSeconds = seconds - secondsDateOfBirth;
NSLog(@"My life in seconds: %f", myLifeInSeconds);

#9

I added something to the challenge: I also calculated the difference in seconds between the day I was born and the day Aaron was born (I assumed that the date in the book is Aaron’s birthday).
To do this, I reused the variables ‘comps’, ‘g’ and ‘d’, I’m wondering if this is something that I can do safely (I know it worked) or wether I better define a new variable… Here’s my code:

[code]int main (int argc, const char * argv[])
{
@autoreleasepool {
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1973];
[comps setMonth:5];
[comps setDay:2];
[comps setHour:10];
[comps setMinute:15];
[comps setSecond:0];

    NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *dateOfBirth = [g dateFromComponents:comps];
    
    [comps setYear:1969];
    [comps setMonth:4];
    [comps setDay:30];
    [comps setHour:13];
    [comps setMinute:10];
    [comps setSecond:0];
    
    NSDate *dateOfBirthAaron = [g dateFromComponents:comps];        
    NSDate *now = [NSDate date];
            
    NSLog(@"The date is %@.", now);
    NSLog(@"I was born on %@", dateOfBirth);

    double d = [now timeIntervalSinceDate:dateOfBirth];
    NSLog(@"It has been %f seconds since I was born.", d);
    
    d = [dateOfBirth timeIntervalSinceDate:dateOfBirthAaron];
    NSLog(@"I was born %f seconds after Aaron was born.", d);
}
return 0;

}[/code]


#10

That is, in fact, my birthday. (It is also Queens Day in Holland.)

Thanks!


#11

sorry, a little off-topic:

I know, that’s why I won the book in Appsterdam, but I didn’t know if I could post that in the open on this forum! :wink:


#12

This was mine: teaching a novice like me Objective C is like teaching the scent of a rose to someone who can’t smell…but Aaron is doing a good job.

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
  
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        [comps setYear:1974];
        [comps setMonth:1];
        [comps setDay:28];
        [comps setHour: 7];
        [comps setMinute:0];
        [comps setSecond:0];
        
        NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDate *dateOfBirth = [g dateFromComponents:comps];
        NSDate *now = [NSDate date];
        double d = [now timeIntervalSinceDate:dateOfBirth];
        NSLog(@"It has been %.0f seconds since your birthday", d);
    }
    return 0;
}

#13

I put a lot of NSLog statements to see the results.
When I run the Xcode>Product>Analyze there was some memory leaks.
So I added the release call for the allocated memory objects.

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
    @autoreleasepool {
        
        /* Create a object named comps */
        NSDateComponents *comps = [[NSDateComponents alloc]init];
        [comps setYear:1969];
        [comps setMonth:4];
        [comps setDay:30];
        [comps setHour:13];
        [comps setMinute:10];
        [comps setSecond:0];
        
        /* Create a object for g */
        NSCalendar *g = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
        //NSLog(@"g => %@", [g dateFromComponents:comps]);
        
        /* set the birthdate */
        NSDate *dateOfBirth =[g dateFromComponents:comps];
        //NSLog(@"dateOfBirth => %@",dateOfBirth);
        
        /* set the date to now */
        NSDate *now = [NSDate date];
        //NSLog(@"now => %@",now);
        
        /* set the difference in seconds */
        double d = [now timeIntervalSinceDate:dateOfBirth];
        //NSLog(@"d => %f",d);
        
        /* display the differance */
        NSLog(@"Aaron has been alive for %f seconds since: %@", d, dateOfBirth);
        
        /* clean house */
        // no memory leaks please
        [g release];
        [comps release];
        
    }// end autorelasepool
    
    return 0;
}// end main

#14

I was stumped for a while when my program was displaying the number of seconds I had been alive as negative. Was I doing things backwards? Nope; the logic of my program seemed to be flawless. Are we in a time warp, maybe? It turns out in my NSLog() line, I was using %d to print the number of seconds (and why not use “d” to print a double?) instead of %f. For whatever reason, this was causing a negative number to be printed. Lesson learned; if the output looks wacky, check that I’m using the right placeholders in my NSLog() call.


#15

I’m getting a negative number for an answer. Is this correct? It seems logical since its going backwards to calculate.


#16

Did you read Albright’s message above yours regarding negative numbers?

Should not be negative.


#17

Well, duh…sorry about that. Thanks for the pointer.


#18

FYI, if you leave the %d alone and place abs() around your pointer in your NSlog statement, you will get the correct answer. (I think correct, because the %f is giving the same answer as the abs() )


#19

And another one - i like it short :smiley: :

[code]#import <Foundation/Foundation.h>

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

@autoreleasepool {
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:9];
    [comps setMonth:10];
    [comps setYear:1965];
    
    NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *dateOfBirth = [g dateFromComponents:comps];
    
    double secondsSinceBirth = [[NSDate date] timeIntervalSinceDate:dateOfBirth];
    NSLog(@"You are %.0f seconds alive.", secondsSinceBirth);
    
}
return 0;

}
[/code]


#20

This is what I got…

[code]{

@autoreleasepool {
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setYear: 1996];
    [comps setMonth: 6];
    [comps setDay: 26];
    
    
    NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *dateOfBirth = [g dateFromComponents:comps];
    NSDate *now = [NSDate date];
    
    double secondsAfterBirth = [now timeIntervalSinceDate: dateOfBirth];
    NSLog (@"It has been %.1f seconds since I was born.", secondsAfterBirth );
    
                   
}
return 0;

}
[/code]

My question is how would you have it just simply print out your birthday (in 4-15-1987 format). Just curious.