开发者

NSString is really Immutable?

开发者 https://www.devze.com 2023-01-10 09:26 出处:网络
Why when I code I can do: NSString *test; test = @\"stack\"; test = @\"overflow\"; without any problems, NSString is not开发者_如何转开发 supposed to be immutable?It is immutable. You\'re not chan

Why when I code I can do:

NSString *test;

test = @"stack";
test = @"overflow";

without any problems, NSString is not开发者_如何转开发 supposed to be immutable?


It is immutable. You're not changing the underlying string with those statements, you're creating/using a brand new string. The pointer itself can change as often as you want but that's not the string.


The string object is immutable. You are only changing the pointer to a different string.


In your code you just "forget" about the old value (stack) and create a new string instance and fill it with "overflow"...

0

精彩评论

暂无评论...
验证码 换一张
取 消