将字符串分配给具有“=”的NSString的是什么?实际上做什么? [英] What does assigning a literal string to an NSString with "=" actually do?

查看:129
本文介绍了将字符串分配给具有“=”的NSString的是什么?实际上做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下行实际上做了什么?

What does the following line actually do?

string = @"Some text";

假设在标题中声明string:

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

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?

谢谢!

作业就是这样。 string 指针基本上是一个指向内存中特定地址的标签。重新赋值语句会将该标签指向内存中的另一个地址!

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!

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

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:

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

但是,字符串文字不需要被管理,因为它们被静态分配,永远不会被释放!因此, release retain 方法只是NOOPs(即没有操作)。您可以安全地忽略它们。

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.

这篇关于将字符串分配给具有“=”的NSString的是什么?实际上做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆