instance variable going out of scope
I have an NSString which has been declared as property and being allocated and initialised in view did load as below:
NSString *textWithKey;
@property(nonatomic,retain)NSString *textWithKey;
in .m i have
@synthesize textWithKey;
and in viewDidLoad i have
self.textWithKey=[[NSString alloc]init];
self.textWithKey=@"";
Now 开发者_如何学Pythonsomewhere in my code i am using
self.textWithKey=[self.textWithKey stringByAppendingString:text1];
it works fine untill another method is called which returns different values. and from there on this perticularline is called again but debugger shows textWithKey out of scope.I have not released textWihKey any where.
Yes that's right. You did not release it. But you also didn't allocate it. ;-) At first you called self.textWithKey = [[NSString alloc] init]
. Than you call self.textWithKey = @""
. Because you use the setters for the property, the old assigned value is released every time. Try the following:
self.textWithKey = [[NSString alloc] initWithString:@""];
Because
self.textWithKey = @"";
is the same as
self.textWithKey = [NSString stringWithString:@""];
And there you don't allocate anything. ;-)
Variables going out of scope are not the same as being released.
精彩评论