Chap 13 Challenge + a question about alloc & init


#1

Hello!
I’ve bought a nice book of yours - Objective C (in german) and i’m trying now to do some Obj-C stuff. And i’m stuck on the 13th chapter. I’ve solved the challenge, but i have no idea, why does it work this way and not the other. Here is what i’m trying to say (well, my english is pretty bad - i’m sorry for that :wink: )

NSTimeZone *tZone2 = [NSTimeZone systemTimeZone];
NSLog(@"Now we have:");
if ([tZone2 isDaylightSavingTime] == YES)
    NSLog(@"summertime!");
else 
    NSLog(@"wintertime!");

This one works perfectly. But my first attempts were something like this:

NSTimeZone *tzone = [[NSTimeZone alloc]init];
NSLog...
if..
and so on

Because i’ve seen those “alloc init” bits everywhere.
As soon as I understand, this alloc init means “allocate some memory and initialize this new object”. And so i decided to put it on every new object or whatever :wink: It didn’t work. The pointer “tzone” returns NULL. So if i would like to log the name of the zone with [tzone name], it would write (null). I think, i should somehow “say” to use “systemTimeZone” method or property, or whatever on the initialization?
But my most important question is - in which cases do i have to use “alloc init” and in which not?

P.S. I’m very sorry for my english :slight_smile:

[EDIT]
Hey SORRY guys! I’ve found everything few threads ahead :confused:
viewtopic.php?f=145&t=3120#p7615

So to manage my problem i had to write something like:

NSTimeZone *tzone = [[NSTimeZone alloc]init];
tzone = [NSTimeZone systemTimeZone];

right?


#2

[quote=“nDeedy”]Hello!
I’ve bought a nice book of yours - Objective C (in german) and i’m trying now to do some Obj-C stuff. And i’m stuck on the 13th chapter. I’ve solved the challenge, but i have no idea, why does it work this way and not the other. Here is what i’m trying to say (well, my english is pretty bad - i’m sorry for that :wink: )

NSTimeZone *tZone2 = [NSTimeZone systemTimeZone];
NSLog(@"Now we have:");
if ([tZone2 isDaylightSavingTime] == YES)
    NSLog(@"summertime!");
else 
    NSLog(@"wintertime!");

This one works perfectly. But my first attempts were something like this:

NSTimeZone *tzone = [[NSTimeZone alloc]init];
NSLog...
if..
and so on

Because i’ve seen those “alloc init” bits everywhere.
As soon as I understand, this alloc init means “allocate some memory and initialize this new object”. And so i decided to put it on every new object or whatever :wink: It didn’t work. The pointer “tzone” returns NULL. So if i would like to log the name of the zone with [tzone name], it would write (null). I think, i should somehow “say” to use “systemTimeZone” method or property, or whatever on the initialization?
But my most important question is - in which cases do i have to use “alloc init” and in which not?

P.S. I’m very sorry for my english :slight_smile:

[EDIT]
Hey SORRY guys! I’ve found everything few threads ahead :confused:
viewtopic.php?f=145&t=3120#p7615

So to manage my problem i had to write something like:

NSTimeZone *tzone = [[NSTimeZone alloc]init];
tzone = [NSTimeZone systemTimeZone];

right?[/quote]

Right - however the first way is also correct NSTimeZone *timeZone = [NSTimeZone systemTimeZone];

Both do the same thing.