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: Harilaos Skiadas <email@hidden>
- Date: Sun, 6 Mar 2005 18:29:49 -0800 (PST)
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
> >Can you translate this for me?
> >
> >NSString *result = [NSString stringWithFormat:@"%@
has %u
> >letters.",string,letterCount];
> >
>
> I'll try. If I mess it up someone will whap me on
the head
>
<-- whaps him on the head. Sorry, I couldn't resist.
-->
> 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:
>
This is right so far.
> NSString *result = [[[NSString alloc]
initWithFormat:@"some
> format",some object] retain];
>
You probably meant ``autorelease'' instead of
``retain'', otherwise the above code would return an
object with too high a retain count, and which object
we are still responsible for releasing in the future.
NSString *result = [[[NSString alloc]
initWithFormat:@"some format",some object]
autorelease];
> 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.
>
The key thing to note is that it is very likely that
the object will end its life cycle soon after the
method containing the above code returns. If you want
it to stick around more, you have to either alloc it
without the extra retain part in Tony's code, or add a
retain part in Tom's code.
> Tony
> 3 Cats And A Mac
> http://www.3caam.com
Haris
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.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