Clarification Needed Regarding retainCount [duplicate]
-
19-06-2021 - |
Question
Possible Duplicate:
NSString retainCount is 2147483647
Let say i have an class named as MyTestClass.h.
There are three NSString variables which are initialized different ways
Class structure is look like
@interface MyTestClass : NSObject {
NSString *testString1;
NSString *testString2;
NSString *testString3;
}
@property (nonatomic, retain) NSString *testString1;
@property (nonatomic, retain) NSString *testString2;
@property (nonatomic, retain) NSString *testString3;
@end
MyTestClass.m
@implementation MyTestClass
@synthesize testString1, testString2, testString3;
-(id) init{
self.testString1 = @"";
[self setTestString2:@""];
testString3 = @"";
}
Now i am planning to create an object of MyTestClass
MyTestClass *obj = [[MyTestClass alloc] init];
I think after this code line execution testString1, testString2 and testString3 retainCounts will be 1.
Am i correct my friends?
may i know that what will happened if i release testString3 ?
Any help on this is appreciated.
Thanks
Solution
Since you are using literal strings in this example retainCount
is of no importance (not even as a delta) since those string objects aren't actually ever going to be dealloc'd.
However, if you were assigning a runtime allocated string to each of your properties, the first two would be retained, the third would not. Since your properties are declared as retain and you're using dot syntax in one and the "setProperty" syntax in the other, the same setter is executed and the object passed to that setter is retained. The third example you're simply setting the backing ivar for the property to the value on the right and not affecting it's ownership.
It's worth noting, retainCount
is only ever useful as a delta and shouldn't be thought of as an explicit number. You should never explicitly check the value of retainCount
, since it's absolute value is unpredictable and of no use. You seem to understand this in your question, but it's worth restating whenever dealing with retainCount to make sure all involved understand it.