Time Zone Challenge


#1

Based on what I read here and some experimenting, I reached the following solution. Works right, but I have no idea why . . . this is my third ( or fourth) attempt to learn Obj-C – from what I’m reading on this forum it seems that those posting here have a better understanding than I. I can grasp what I read of C in the first part of the book, but this OPP is still a great big fog . . .

Will appreciate all comments . . . thanks.

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

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSTimeZone * theCurrentTime = [NSTimeZone systemTimeZone];{
if ([theCurrentTime isDaylightSavingTime]){
    NSLog(@"You are in Daylight Savings Time.");
} else
        NSLog(@"You are in Standard Time.");
    
}

[pool drain];
return 0;

}[/code]

josf


#2

BTW, I forgot to mention that I’m using XCode 4.0.2, that’s why the “NSAutoReleasePool * pool, etc.” is the old way . . .

josf


#3

Don’t let the OOP lingo turn you off. If you are comfortable (in C) with the concept of a struct, function, pointer (including pointer to function), Objective-C should feel like a breeze. Of course there is some difference in syntax, but that’s just syntax and should become transparent as you gain more experience. Don’t give up! Go back to the beginning of the book and start reading again, with an open mind; If you don’t understand anything, just ask and don’t be afraid of doing it :slight_smile:

Remember this: Aaron is not only a great teacher but he has done an excellent job writing this book. (I have not met him personally but I have read all his books!)


#4

Thanks for the encouragement . . . It is definitely the syntax . . . I will do exactly what you suggest and re-read the front of the book.

Well, I don’t really see how my “solution” worked. Why does it work without the comparison in the conditional statement? - for instance. It is obvious and I just don’t see it?

josf


#5

What do you mean by your question: Why does it work without the comparison in the conditional statement? Could you be more explicit?


#6

According to the book isDaylightSavingTime is an instance method of NSTimeZone and returns YES if Daylight Savings is the current time zone. I took it at face value and simply made my conditional statement as you see here:
if ([theCurrentTime isDaylightSavingTime]){
NSLog(@“You are in Daylight Savings Time.”) . . . etc.

You can see it in context in my OP.

I sort of get it but not quite. I’d like to see how the method was declared. By comparison in the conditional statement, I just meant why isn’t there a need for == as, for example, if ( x == y+z) etc.

I also wonder why I didn’t see anyone else use that simple method call . . . although I don’t think I read every solution . . .

So I guess what I’m saying is, I still need to work through these methods as a concept in general, as against the function calls I’m more used to.

josf


#7

What you have done is correct and is equivalent to this:

if ([theCurrentTime isDaylightSavingTime] == YES){ // Say Daylight Savings Time. } else { // Say Not Daylight Savings Time. }

If you prefer to test explicitly, you can always write your conditionals like this: if (x == YES){...}else{...}

By the way, there is really no big difference between a function call and a method invocation. The compiler will transform every method invocation into a function call on your behalf.

For example, the method invocation: [theCurrentTime isDaylightSavingTime]

could end up being transformed into something like this: invoke_instance_method (theCurrentTime, “isDaylightSavingTime”)

If you are curious about the exact details, you need to do some deep reading. It is actually fun (and worth the effort) reading Apple’s Objective-C documents. :slight_smile:

For example: Take a look at the section named “Messaging” in “Objective-C Runtime Programming Guide.”


#8

That’s an interesting point you make that the method invocation is transformed into a function call . . . that’s a neat way to think of it.

I will go through the book again, and I will read the part of the programing guide you suggest also.

Thanks for your comments and advice . . . back to the book.

josf