Re: Learning Cocoa/ObjectiveC and wondering why I had to do this.
Re: Learning Cocoa/ObjectiveC and wondering why I had to do this.
- Subject: Re: Learning Cocoa/ObjectiveC and wondering why I had to do this.
- From: Tony Cate <email@hidden>
- Date: Sun, 6 Mar 2005 17:02:58 -0600
>Can you translate this for me?
>
>NSString *result = [NSString stringWithFormat:@"%@ has %u
>letters.",string,letterCount];
>
>It did what I wanted, which was create a string that is the results of
>a string, an unsigned int and some other words and put them in there.
>
>But it didn't do it the way I thought I should. I can't figure out
>why I had to put the NSString inside the object call [NSString
>stringWithFormat...]
>
>Was doing the challenge in that one Big Nerd Ranch cocoa book and got
>lost a bit.
>
>My C skills are rusty, it's been a few years. So I apologize for the
>blatant newb post.
I'll try. If I mess it up someone will whap me on the head
NSString *result - declares the obect but it doesn't actually exist until...
= [NSString stringWithFormat... creates the object and stuffs a value in it. Which you already knew.
NSString is a factory Class object. stringWithFormat is a message to that factory to create a new object which, by the way, has some specific stuff in it.
You could achive the same thing with this syntax:
NSString *result = [[[NSString alloc] initWithFormat:@"some format",some object] retain];
The format you used is referred to, I believe, as a convenience method. It's convenient because you don't have to do the 'alloc' piece.
Tony
3 Cats And A Mac
http://www.3caam.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden