Re: Synthesize NSMutableString retain count question
Re: Synthesize NSMutableString retain count question
- Subject: Re: Synthesize NSMutableString retain count question
- From: Ryan Homer <email@hidden>
- Date: Wed, 17 Feb 2010 09:05:56 -0500
On 2010-02-03, at 6:55, Philip Vallone wrote:
> Hi List,
>
> This is probably a very elementary question. I have a NSMutableString that I Synthesize. When I assigned a value to it:
>
> currentSection = @"Some value";
You should probably use self.currentSection instead of just currentSection to ensure that your setter is being used. That is why you synthesize, after all, so that the compiler will write getter and setter methods for you.
The way that you did it, you are assigning @"Some value" directly to the instance variable without using the setter.
If you do reference a variable using dot notation, your accessors won't be used. Dot notation does not always have to include self, for example:
someInstanceObject.someProperty = someValue;
self.someInstanceVariable = someValue;
But if you have a single ivar, such as currentSection and you want to use your accessor, you need to prefix it with self.
>
> The retain count goes to 1
It is my experience that you can't really count on the retainCount to debug your programs. You're better of using Xcode's "Run->Enable Guard Malloc" or "Run->Run with Performance Tool->Leaks"
>
> However if I assign a value with
>
> [currentSection setString:@"Some value"];
Here, you've not assigned anything. If currentSection is currently nil, that you are effectively doing:
[nil setString:@"Some value"]
which does nothing at all. You probably meant to do something like this:
self.currentSection = [NSString string]; // allocate the memory for the string
[self.currentSection setString:@"Some value"]; // set the string to the allocated memory
Though this is superfluous and you'd probably never see this.
>
> The retain count is still zero.
>
> Can someone explain the difference?
>
> Thanks,
>
> Phil
_______________________________________________
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