2

Possible Duplicates:
NSString retain Count
Objective C NSString* property retain count oddity
When to use -retainCount ?

Why does this code show the retain value greater than 1? And why is it 2147483647?

NSString *data22 = [[NSString alloc] initWithString:@"fsdfsfsdf"];

int a = [data22 retainCount];
NSLog(@"retain count 1== %d  ====" ,a);

The output of the above code is

 retain count 1== 2147483647  ====
Community
  • 1
  • 1
Iqbal Khan
  • 4,587
  • 8
  • 44
  • 83

1 Answers1

13

It is 2147483647 because you looked. Don't look and it will be the value you expect.

Seriously. Don't call retainCount. Not ever. It is useless.

Why it is such a ridiculous number is because of an implementation detail. @"..." is a constant string. NSString can recognize constant strings and decides that your particular code has no need of a second space consuming copy of a constant immutable string and, thus, returns the already existing constant string.

I.e. a singleton. Of a class whose instances are only ever created by the compiler. For which retain/release/autorelease/retainCount are utterly devoid of meaning.

As for why it is 2147483647, a picture is worth a thousand words. Or, in this case, 31 set bits. negative one expressed in far too many digits

bbum
  • 162,346
  • 23
  • 271
  • 359