Challenge difficulties


#1

Hey, I’m having slight difficulties with completing the challenge, which is a shame as I really felt my understanding of this chapter was sufficient to complete it. I have created the following code, which seems right to me based on the information earlier in the chapter but is obviously incorrect as the output simply creates (null) as opposed to yes or no. I had a quick peep at other people solutions, which showed much more complex answers than I have, which shows I am missing something really fundamental, but I didn’t want to look properly as I still wish to properly complete the challenge. I was wondering if anyone could take a look and point me in the right direction.

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

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

@autoreleasepool {
    
    NSTimeZone *timezone = [[NSTimeZone alloc] init];
    NSLog(@"The time zone I am in is %@", timezone);
    
    char savingtime = [timezone isDaylightSavingTime];
    NSLog(@"%@ it is currently daylight saving time", savingtime);
}
return 0;

}

[/code]


#2

Two problems with your code.

First, you can’t rely on the normal alloc/init mechanism for this class. You need to ask the class to give you a new object pre-initialized with the local time zone. Apple’s documentation doesn’t say that you can’t use alloc/init, but it does say:

The second problem is that %@ in NSLog() is only valid for objects, and char is not an object, it’s a simple data type. (Incidentally, using char for a true/false value is not generally used in Objective-C as far as I can tell; use BOOL instead.)

Here’s a version of your code that works, mostly:

        NSTimeZone *timezone = [NSTimeZone localTimeZone];
        NSLog(@"The time zone I am in is %@", timezone);
        
        BOOL savingtime = [timezone isDaylightSavingTime];
        NSLog(@"%d it is currently daylight saving time", savingtime);

At this point, it’s still a little quirky because the second output line (for the next few weeks in the US) reads: “0 it is currently daylight saving time”. Still, it captures the essence of what you want, that the savingtime value is 0, or false.


#3

Oh I see. In regards to the first bit of code: NSTimeZone *timezone = [NSTimeZone systemTimeZone]; NSLog(@"The time zone I am in is %@", timezone);
I actually had that written before, I just was frantically trying alternatives and mistakenly copied the allec version over when I decided to ask for help. So thats cool.

So am I right in thinking that what I was missing was the correct type to store the variable savingtime at (I’ll be honest, I never would have guessed BOOL), and then consequentially the correct token to be able to print this type. From there, with the result false, would it be right to think to create an if else statement to be able to present the result within a desired format (e.g. “No it is not daylight saving time”), which would then be the if else part of the code that I saw in everyone else’s results that intimidated me so much.

So doing such thins would present me with the following solution:

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

@autoreleasepool {
    
    NSTimeZone *timezone = [NSTimeZone systemTimeZone];
    NSLog(@"The time zone I am in is %@", timezone);
    
    BOOL savingtime = [timezone isDaylightSavingTime];
    if (savingtime == 1) {
        NSLog(@"It is currently daylight saving time");
    } else {
        NSLog(@"It is not currently daylight saving time");
    }
}
return 0;

}

[/code]

Does this all seem like logical, ‘programmer like’ steps for me to have taken to arrive at the correct solution?


#4

Here is what works for me:

// Challenge 2
NSTimeZone *myTimeZone = [NSTimeZone localTimeZone] ;
NSLog(@"Timzone : (%@)", myTimeZone);
NSLog(@"DST?    : (%d)", [myTimeZone isDaylightSavingTime]);

The problem is, that I get “1” instead of “Yes” in the second NSLog statement:

2012-09-01 07:08:52.907 TimeAfterTime[3450:303] Timzone : (Local Time Zone (America/Toronto (EDT) offset -14400 (Daylight)))
2012-09-01 07:08:52.907 TimeAfterTime[3450:303] DST?    : (1)

Otherwise, it seems fine.