Wednesday, December 25, 2013

Expert answer, NSString strange question,


NSString * s1 = @ "ssgsdgsdgsdg";

NSLog (@ "% d", [s1 retainCount]); / / -1

NSString * s2 = [s1 copy];


NSLog (@ "% d", [s2 retainCount]); / / -1


Why is -1, the reason is: in a similar C + + string is stored in the constant region, right.
----------------------------------

But how to explain the following code?


NSString * s1 = [[NSString alloc] init];


NSLog (@ "% d", [s1 retainCount]);

The result is -1, I did not release it with Mandarin, as long as alloc message, it becomes a why to -1, explain, thank you.


NSString * s = [[NSString alloc] initWithString: @ "nihao"];

NSLog (@ "% @", s);


NSLog (@ "% d", [s retainCount]); / / -1 -1 is also why,

[S release];

NSLog (@ "% d", [s retainCount]); / / -1





Reply:
I do not know why you tangle retaincount
Reply:
The reason I tangled, because they are aware memory management which chapter, if not zero, then the memory is not freed.


I think this problem very seriously, certainly I do not understand the essence of memory management.

So just ask.


Welcome to the expert guidance

cited a floor arthurchenjs reply:
do not know why you tangle retaincount

Reply:
retaincount the system to automatically subtract the bar
Reply:
NSString constant region is, it will not be automatically released storage. This and the constant string c language is the same!
Reply:
references, 4th Floor wpalhm reply:
NSString is in the constant region, it is not automatically free up memory. This and the constant string c language is the same!
not exactly right, if it is initWithFormat, it has this method, it is necessary to release, as used in the alloc
Reply:
Weighs, I was the first time I saw retaincount will equal -1, 0 when the system is not to say that will be automatically released yet?
-1 How to explain?
Is this thing still object credit for?

This value is -1 if there really that I have to go take a look at this thing a memory management
Reply:
In the constant region, will not be released automatically NSString storage.
Reply:
lz you have problems. . . Retaincount already saying this is not very accurate

No comments:

Post a Comment