Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is NSString retain count 2?

#define kTestingURL @"192.168.42.179"

...

NSString *serverUrl = [[NSString alloc] initWithString:
                        [NSString stringWithFormat:@"http://%@", kTestingURL]]; 
NSLog(@"retain count: %d",[serverUrl retainCount]);

Why is the retain count 2 and not 1?

like image 547
Man of One Way Avatar asked Dec 13 '22 10:12

Man of One Way


1 Answers

Yes, You will get retain Count 2, one for alloc and other for stringWithFormat. stringWithFormat is a factory class with autorelease but autorelease decreases retain count in the future.

like image 163
iCoder4777 Avatar answered Feb 02 '23 20:02

iCoder4777