Chapter 13 Challenge Question


#1

I’m working through the book, doing the challenges as well. This one has me a little stumped.

        printf("\n======= Challenge 13 ========\n");
        BOOL isDST = [[NSTimeZone systemTimeZone] isDaylightSavingTime];
        
        NSLog(@"Is it currently daylight savings time? %c", isDST); 
        NSLog(@"What time zone is it? %@",[NSTimeZone systemTimeZone]);

The output I get is below, I can’t seem to figure out a way to print the BOOL result directly. If I try %d in as the token it’s a 0, which I think is right since I’m working it the day after the change, but I’d like it to say “NO”. I followed the definition of BOOL to

Also, isDaylightSavingTime shows a return type here:

======= Challenge 13 ======== 2011-11-06 15:08:34.985 TimeAfterTime[29998:707] Is it currently daylight savings time? 2011-11-06 15:08:34.986 TimeAfterTime[29998:707] What time zone is it? America/New_York (EST) offset -18000 Program ended with exit code: 0


#2

Yes, 0 is false and 1 is true.

For pretty output, make an if statement:

if (isDST) { NSLog(@"Yes, it is daylight saving"); } else { NSLog(@"No, it is not daylight saving"); }


#3

here is mine:

    NSTimeZone *s=[NSTimeZone systemTimeZone];
    
    NSLog(@"THE TIME ZONE IS: %@",s);
    
    
   BOOL val= [s isDaylightSavingTime];
    
    if(val){
        NSLog(@"Day Light is on");
    }
    else {
        NSLog(@"Day Light is off");
    }

#4

Hi, I’ve write a solution for the challenge question, but it doesn’t work, and I don’t know why.
It’s a very simple solution, but I think that it should work.
Why do you think that the next code is wrong?

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
        NSLog(@"Daylight Saving Time Zone? %c.", [timeZone isDaylightSavingTime]);
    }
    return 0;
}

#5

%c is for characters. isDaylightSavingTime returns either 0 or 1. This number is interpreted as an ASCII character, which is unprintable.

Use %d instead. Or, better yet, an if-statement like some of the other solutions.


#6

[quote=“AaronHillegass”]%c is for characters. isDaylightSavingTime returns either 0 or 1. This number is interpreted as an ASCII character, which is unprintable.

Use %d instead. Or, better yet, an if-statement like some of the other solutions.[/quote]

Ok, thank you!
I thought that the boolean variables return YES or NO characters.


#7

thanks for this forum! me too, i thought it’ll return YES or NO… :slight_smile:


#8

I had my own special interpretation of the Challenge. I tried to have my program answer the Daylight Savings Time with the actual word “YES” stored in the XCode. All the solutions I see in the forum work around this issue with If/Else frames. ex. if ([currentTimeZone isDaylightSavingsTime] == YES… or the Boolean if (val). I was stuck because I was going nuts trying to make the code actually state YES since this is what I thought Aaron wanted. I made a pointer variable NSTimeZone *zone_type = [NSTimeZone systemTimeZone]; Then I tried %c or %s formats to state YES. Thus:
char confirm = [zone_type isDaylightSavingTime];
NSLog(@“Are we in DST?” %c", confirm; The %s failed completely, and the %c returned a mysterious upside-down question mark character. I even tried %p which returned the hex x01.

Anyway, is there a way to retrieve that elusive “YES”?


#9

There is no easy way to print a YES or NO string from a value of type BOOL in the Objective-C language, because a value of type BOOL is an integer number, not a character nor a sequence of characters (text).

You have, however, several options at your disposal:
viewtopic.php?f=135&t=5083


#10

I tried this example a number of ways, each of which worked. I am still a little confused about when we need to allocate and initialize memory. See below. Is it ok to use any of the three ways and is there a right way.

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        //This worked:
        //NSTimeZone *myTimeZone = [[NSTimeZone systemTimeZone]init];
        
        //This worked:
        //NSTimeZone *myTimeZone = [NSTimeZone systemTimeZone];
        
        //This worked
        NSTimeZone *myTimeZone = [[NSTimeZone alloc]init];
        myTimeZone = [NSTimeZone systemTimeZone];
        
        int isDaylightSavings = [myTimeZone isDaylightSavingTime];
        
        if (isDaylightSavings) {
            NSLog(@"This system is set to daylight savings time");
        } else {
            NSLog(@"This system is not set to daylight savings time");
        }
        
        NSLog(@"THE TIME ZONE IS: %@",myTimeZone);
        
    }
    return 0;
}

#11

At this level of discourse, it is better to say allocate and initialise an object than allocate and initialise memory .

[NSTimeZone systemTimeZone] will give you a fully initialised object. Initialising it again may result in undesirable behaviour.

You should read the class reference first before attempting to be adventurous.


#12

Thank you very much, your explanation was very helpful.


#13

Question about pointer or regular variable.

At first I had BOOL *daylight, however I quickly found that it was returning a value, not assigning a value to that reference in memory.

How would I normally know if the object needs a variable or pointer as the ‘return’ variable.
Basically how does one know that it is BOOL daylight, not BOOL *daylight?

Thanks


#14

By finding and reading the relevant documentation, for example class references; by looking at the header files.

This is very easy to do in Xcode:

  • Option click on any class type, for example NSString, NSArray, etc; or
  • Open the Utilities pane; select the Show Quick Help inspector; then click on anything in the code to display help in the inspector.

You must get into the habit of reading and researching if you wan’t to be a happy coder.


#15

This was my solution:

[code] @autoreleasepool {

    NSTimeZone *seattle = [NSTimeZone systemTimeZone];
    
    if (seattle.isDaylightSavingTime) {
        NSLog(@"It is daylight savings");
    } else {
        NSLog(@"It isn't daylight savings");
    }
}[/code]

It seemed to be successful and concise. But I’m concerned that I didn’t do it right. Was the way I did the if statement right?


#16

This is my solution, which uses the ternary operator in the NSLog

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

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

@autoreleasepool {
    
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone]; // Make an instance of the time zone
    NSLog(@"The time zone is %@", timeZone);            // Print the time zone
    
    BOOL isItDayLightSaving = [timeZone isDaylightSavingTime];  // Is it day light saving?
    // Here we use the ternary operator instead of an if statement
    NSLog(@"Is it day light saving? %@", isItDayLightSaving ? @"Yup, it is!" : @"Nope, not yet.");
}
return 0;

}[/code]


#17

[code]@autoreleasepool {

    NSTimeZone *timeZone = [[NSTimeZone alloc] init];
    
    timeZone = [NSTimeZone systemTimeZone];
    
    BOOL isDaylight = [timeZone isDaylightSavingTime];
    
    NSString *text = [[NSString alloc] init];
    
    text = @"The time zone of this Mac is";
    
    NSString *daylightDescription = [[NSString alloc] init];
    
    if (YES == isDaylight) {
        daylightDescription = @"The time zone is currently in daylight saving time.";
    }else if (NO == isDaylight){
        daylightDescription = @"The time zone is not currently in daylight saving time.";
    }
    
    NSLog(@"%@ %@. %@", text, timeZone, daylightDescription);
}[/code]

[Moderator Edit] Added Code tags so that code would be formatted.


#18

[quote=“mmerkes”]This was my solution:

[code] @autoreleasepool {

    NSTimeZone *seattle = [NSTimeZone systemTimeZone];
    
    if (seattle.isDaylightSavingTime) {
        NSLog(@"It is daylight savings");
    } else {
        NSLog(@"It isn't daylight savings");
    }
}[/code]

It seemed to be successful and concise. But I’m concerned that I didn’t do it right. Was the way I did the if statement right?[/quote]

Normally there is no real “right” and “wrong” ways to solve problems in programming. The old saying goes: “There is more than one way to skin a cat” - the saying is true for programming too.
Your solution is just fine.


#19

I like the ternary operator.

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

@autoreleasepool {
    
    NSTimeZone *tz = [NSTimeZone systemTimeZone];
    NSLog(@"Are we in DST? %c", [tz isDaylightSavingTime] ? 'Y' : 'N');
    
}
return 0;

}[/code]


#20

A quick Google search found me an elegant solution to displaying a BOOL as “Yes” or “No”:

Complete code to my solution:


#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        NSTimeZone *here = [NSTimeZone systemTimeZone];
        
        bool isSavingsNow = [here isDaylightSavingTime];
        
        NSLog(isSavingsNow ? @"Yes" : @"No");
               
    }
    return 0;
}

source:
stackoverflow.com/questions/6358 … g-in-nslog