将文字字符串分配给带有“=”的NSString实际上做了什么?

时间:2021-09-07 20:18:55

What does the following line actually do?

以下行实际上做了什么?

string = @"Some text";

Assuming that "string" is declared thusly in the header:

假设在标题中声明了“string”:

NSString *string;

What does the "=" actually do here? What does it do to "string"'s reference count? In particular, assuming that for some reason "string" is not otherwise assigned to, does it need to be released?

“=”实际上在这里做了什么?它对“字符串”的引用计数有什么作用?特别是,假设由于某种原因“字符串”没有另外分配,是否需要释放?

Thanks!

3 个解决方案

#1


20  

The assignment is just that. The string pointer is basically a label that points to specific address in memory. Reassignment statement would point that label to another address in memory!

任务就是这样。字符串指针基本上是指向内存中特定地址的标签。重新分配语句会将该标签指向内存中的另一个地址!

It doesn't change reference counting or do anything beyond that in Objective-C. You need to maintain the reference count yourself, if you are running in a non-garbage-collection environment:

它不会更改引用计数或在Objective-C中执行任何操作。如果您在非垃圾收集环境中运行,则需要自己维护引用计数:

[string release];
string = [@"Some text" retain];

However, string literals don't need to be managed, as they get allocated statically and never get deallocated! So the release and retain methods are just NOOPs (i.e. no operations). You can safely omit them.

但是,不需要管理字符串文字,因为它们是静态分配的,永远不会被释放!因此,释放和保留方法只是NOOP(即没有操作)。你可以安全地省略它们。

#2


2  

What does the following line actually do?

以下行实际上做了什么?

string = @"Some text";

Assuming that "string" is declared thusly in the header:

假设在标题中声明了“string”:

NSString *string;

What does the "=" actually do here? What does it do to "string"'s reference count?

“=”实际上在这里做了什么?它对“字符串”的引用计数有什么作用?

string is not a string.

string不是字符串。

string is, in fact, not any other kind of Cocoa object, either.

事实上,string也不是任何其他类型的Cocoa对象。

string is a variable, which you've created to hold an instance of NSString. The assignment operator puts something into a variable*. In your example above, you create a literal string, and put that into the variable.

string是一个变量,您创建该变量来保存NSString的实例。赋值运算符将某些东西放入变量*中。在上面的示例中,您将创建一个文字字符串,并将其放入变量中。

Since string is a variable, not a Cocoa object, it has no reference count.

由于string是一个变量,而不是Cocoa对象,因此它没有引用计数。

Assigning an object somewhere can extend the object's lifetime in garbage-collected code (only on the Mac). See the Memory Management Programming Guide for Cocoa for more details.

在某处分配对象可以在垃圾收集的代码中扩展对象的生命周期(仅在Mac上)。有关更多详细信息,请参阅“Cocoa内存管理编程指南”。

*Or a C array. Don't confuse these with Cocoa arrays; they're not interchangeable, and you can't use the assignment operator to put things into a Cocoa collection (not in Objective-C, anyway).

*或C阵列。不要将它们与Cocoa数组混淆;它们不可互换,你不能使用赋值运算符将东西放入Cocoa集合中(不管怎么说都不是在Objective-C中)。

#3


0  

When you use a literal like in this case, it is just syntactic sugar to quickly create an NSString object. Once created, the object behaves just like another other. The difference here is that your string is compiled into the program instead of created dynamically.

当你在这种情况下使用文字时,快速创建NSString对象只是语法糖。一旦创建,该对象就像另一个一样。这里的区别在于您的字符串被编译到程序中而不是动态创建。

#1


20  

The assignment is just that. The string pointer is basically a label that points to specific address in memory. Reassignment statement would point that label to another address in memory!

任务就是这样。字符串指针基本上是指向内存中特定地址的标签。重新分配语句会将该标签指向内存中的另一个地址!

It doesn't change reference counting or do anything beyond that in Objective-C. You need to maintain the reference count yourself, if you are running in a non-garbage-collection environment:

它不会更改引用计数或在Objective-C中执行任何操作。如果您在非垃圾收集环境中运行,则需要自己维护引用计数:

[string release];
string = [@"Some text" retain];

However, string literals don't need to be managed, as they get allocated statically and never get deallocated! So the release and retain methods are just NOOPs (i.e. no operations). You can safely omit them.

但是,不需要管理字符串文字,因为它们是静态分配的,永远不会被释放!因此,释放和保留方法只是NOOP(即没有操作)。你可以安全地省略它们。

#2


2  

What does the following line actually do?

以下行实际上做了什么?

string = @"Some text";

Assuming that "string" is declared thusly in the header:

假设在标题中声明了“string”:

NSString *string;

What does the "=" actually do here? What does it do to "string"'s reference count?

“=”实际上在这里做了什么?它对“字符串”的引用计数有什么作用?

string is not a string.

string不是字符串。

string is, in fact, not any other kind of Cocoa object, either.

事实上,string也不是任何其他类型的Cocoa对象。

string is a variable, which you've created to hold an instance of NSString. The assignment operator puts something into a variable*. In your example above, you create a literal string, and put that into the variable.

string是一个变量,您创建该变量来保存NSString的实例。赋值运算符将某些东西放入变量*中。在上面的示例中,您将创建一个文字字符串,并将其放入变量中。

Since string is a variable, not a Cocoa object, it has no reference count.

由于string是一个变量,而不是Cocoa对象,因此它没有引用计数。

Assigning an object somewhere can extend the object's lifetime in garbage-collected code (only on the Mac). See the Memory Management Programming Guide for Cocoa for more details.

在某处分配对象可以在垃圾收集的代码中扩展对象的生命周期(仅在Mac上)。有关更多详细信息,请参阅“Cocoa内存管理编程指南”。

*Or a C array. Don't confuse these with Cocoa arrays; they're not interchangeable, and you can't use the assignment operator to put things into a Cocoa collection (not in Objective-C, anyway).

*或C阵列。不要将它们与Cocoa数组混淆;它们不可互换,你不能使用赋值运算符将东西放入Cocoa集合中(不管怎么说都不是在Objective-C中)。

#3


0  

When you use a literal like in this case, it is just syntactic sugar to quickly create an NSString object. Once created, the object behaves just like another other. The difference here is that your string is compiled into the program instead of created dynamically.

当你在这种情况下使用文字时,快速创建NSString对象只是语法糖。一旦创建,该对象就像另一个一样。这里的区别在于您的字符串被编译到程序中而不是动态创建。