Неизменный NSString позволяет изменять свои значения?

Как упомянуто, это действительно зависит от того, о какой структуре Вы говорите. Например, можно записать ограниченной очереди без блокировок, но не той, которая предоставляет произвольный доступ.

5
задан Niels Castle 26 November 2009 в 20:18
поделиться

3 ответа

Perhaps the following example will add upon the replies from Mark and Niels and help clarify things.

Immutable strings

// Setup two variables to point to the same string
NSString * str1 = @"Hello World";
NSString * str2 = str1;

// "Replace" the second string
str2 = @"Hello ikilimnik";

// And list their current values
NSLog(@"str1 = %@, str2 = %@", str1, str2);

Mutable strings

// Setup two variables to point to the same string
NSMutableString * str1 = [NSMutableString stringWithString:@"Hello World"];
NSMutableString * str2 = str1;

// "Replace" the second string
[str2 setString:@"Hello ikilimnik"];

// And list their current values
NSLog(@"str1 = %@, str2 = %@", str1, str2);

Notice when you use the immutable NSString class that the only way to "replace" a string is to create a new string and update your variable "str2" to point to it. This however doesn't affect what "str1" is pointing to, so it will still reference the original string.

In the NSMutableString example, we don't create a second string, but instead alter (mutate) the contents of the existing "Hello World" string. Since both variables continue to point to the same string object, they will both report the new value in the call to NSLog.

It's important to differentiate between a pointer variable and the actual object it points to. A NSString object is immutable, but that doesn't stop you from changing the value of a variable which points to a string.

The data type "NSString *" is a pointer to a NSString object, not the object itself. If you set a break point at either of the NSLog statements within the XCode debugger, you may like to check the raw value of each variable to clarify this.

21
ответ дан 18 December 2019 в 05:24
поделиться

Вы не изменяете строку, вы просто переназначаете переменную test, чтобы она указывала на другую строку. Исходная строка не была изменена.

8
ответ дан 18 December 2019 в 05:24
поделиться

TEST и TEST2 - это две разные строки, на которые указывает указатель * test.

Вы не изменяете содержимое явно выделенного объекта NSString из первой строки кода, а указываете * test на другой объект.

8
ответ дан 18 December 2019 в 05:24
поделиться
Другие вопросы по тегам:

Похожие вопросы: