small medium large xlarge

Img_0489_pragsmall
10 Feb 2009, 20:58
Alexander (15 posts)

Bill:

I love your intro to Objective-C. I am a newbie to Objective-C, so if my programming vocabulary isn’t corrent please forgive me.

My question for the Episode 1 is; how do you pass instances to other classes? Here is what I have in the main.m file:

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

NSDate *before = [[NSDate alloc] init];
sleep(3); 
NSDate *now = [[NSDate alloc] init];

  
NSLog(@"Today's date is: %@, but it was %@", now, before, now);
NSLog(@"Earlier date was: %@", [before earlierDate:now]);
[pool drain]; 
return 0; }

What I would like to do is to format the date using NSDateFormatter and pass the now instance to it.

Also, can you say something about instance’s scope too? I am still not getting it.

Thank you for your help,

-Alex

Biopic_100x100_pragsmall
10 Feb 2009, 21:15
Bill Dudney (917 posts)

Hi Alex,

Thanks for the kind words!

This code should work.

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSDate *before = [[NSDate alloc] init];
    sleep(3); 
    NSDate *now = [[NSDate alloc] init];

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]  autorelease];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    NSString *nowString = [dateFormatter stringFromDate:now];

    NSLog("Today's date is: %@, but it was %@", nowString, before);
    NSLog("Earlier date was: %@”, [before earlierDate:now]);
    [pool release];
    return 0;
}

If it does not make sense please ping me back

Img_0489_pragsmall
10 Feb 2009, 21:24
Alexander (15 posts)

Bill:

That was a fast reply!!! Thank you so much!

Coming from Ruby, Objective-C is really quite different, but I can see that it’s not too bad to learn it either…

What you did in your code example was to instantiate a new NSDateFormatter first, then create a new NSString that the ‘now’ instance is passed to it in order to convert numbers to string. Am I correct? Again I appologies for my vocabulary.

-Alex

Biopic_100x100_pragsmall
10 Feb 2009, 22:25
Bill Dudney (917 posts)

Hi Alex,

You bet!

The steps;

Create a dateFormatter and configure it (the setDateStyle: and setTimeStyle: methods)

# ask the dateFormatter for a new string object constructed based on the configuration (no time and medium date) and the date object passed in # assign the newly created string to the nowString variable.

Hope that helps!

Img_0489_pragsmall
10 Feb 2009, 22:36
Alexander (15 posts)

Bill:

It helps a lot!

Thank you!

-Alex

P.S. I am sure I’ll be asking you more questions as I keep watching your movies, and trying to digest overwhelming information, but for the time being this is a good start… Hope that’s ok with you? Also, got your iPhone videos too and am loving it!!! Again, thanks for your hard work and wonderful movies!

Biopic_100x100_pragsmall
11 Feb 2009, 03:23
Bill Dudney (917 posts)

Great Alex,

I’m glad it helps. That is what I’m here for.

Make sure to let us know when you get your first app in the store!

You must be logged in to comment