Re: NSString initWithFormat and stringWith
Re: NSString initWithFormat and stringWith
- Subject: Re: NSString initWithFormat and stringWith
- From: Sherm Pendley <email@hidden>
- Date: Wed, 27 May 2009 11:14:26 -0400
On Wed, May 27, 2009 at 9:59 AM, Ignacio Enriquez <email@hidden> wrote:
>
> 1.- What is the difference between string1 and string2? where
> NSString *string1 = [NSString stringWithFormat:@"myFirstString"];
> NSString *string2 = [[NSString alloc] initWithFormat:@"mySecondString"];
You own string2 and must release it when you're done with it.
You do not own string1 and must not release it.
> I thought that string1's memory allocation and release would be done
> by the system (autoreleased) and string2's memory and release should
> be done by me (by [string2 release])
> Am i mistaking?
Yes, you are mistaken. :-(
The memory contract concerns ownership - i.e. what are the calling code's
responsibilities with respect to retain and/or release. Many objects that
you do not own are in fact autoreleased, but there is no guarantee of that.
The only guarantee concerns whether the caller owns the returned object.
That contract works both ways; the called code is also required to return an
object that behaves correctly if the caller fulfills its end of the
contract. *How* the called method does so is an implementation detail that's
not part of the contract, and something the caller need not care about.
A funny thing is when doing:
> NSLog(@"retainCount %i %i", [string1 retainCoung], [string2 retainCount]);
> I got :
> "retainCount 2147483647 2147483647"
>
> So It seems that both objects are autorelease objects... Why is that?
> I thougth that string2 retainCount would be 1.
With no placeholders in your format, NSString is perfectly within its rights
to return a singleton instance that represents the constant static string
that's compiled into your binary. For such singletons, the retain count is
often set to a "special" value that the -release method will recognize as
meaning "can't touch this." So no, these objects are not autoreleased.
But, it doesn't matter. All that your code needs to do is follow the
contract, and release string2 when it's done with it.
2.- How can I get two simple NSString instances with a retainCount equal to
> 1
You can't depend on any particular value of retainCount. You shouldn't even
be looking at it - as you've seen, it's an internal implementation detail
that can (and does) have values that are only meaningful if you're
maintaining the target class - in this case, NSString.
Take a step further back - what problem are you having, that led you to
think that looking at retainCount would help you solve it?
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden