Objective-C / UIButton / SetTitle

发布于 2024-09-01 00:39:06 字数 185 浏览 7 评论 0原文

UIButton 的 setTitle 方法是否保留作为参数传递的 NSString ?

我想我可以相信该属性定义为:

property(nonatomic,readonly,retain) UILabel *titleLabel

在这种情况下,我认为它确实保留了字符串。

谢谢, 苹果92

Does the setTitle method of UIButton retain the NSString passed as argument ?

I guess I can rely on the fact that the property is defined as:

property(nonatomic,readonly,retain) UILabel *titleLabel

In this case, I think that it does retain the string.

Thanks,
Apple92

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

汹涌人海 2024-09-08 00:39:06

除非另有说明,否则您可以依靠框架类来保留它们所需的内容。这是 Cocoa 内存管理规则的一部分。您应该阅读该文档并让它深入人心,因为一旦您理解了它,基本上所有内存管理问题都会消失 - 相反,如果您不花时间理解该文档,您将始终感到不确定。幸运的是,这非常简单。

另外,我会小心不要过度依赖其他人告诉你的规则。尤其是在像 Stack Overflow 这样的非正式场合,人们倾向于简单化和说错话,从而导致误解——与此同时,官方规则多年来已经被校对了很多很多次。

You can count on framework classes to retain things that they need unless otherwise documented. This is part of Cocoa's memory management rules. You should read that document and let it sink in, because once you understand it, basically all of your memory management questions go away — conversely, if you don't take the time to understand that document, you'll always feel uncertain. Luckily, it's pretty simple.

Also, I would be careful about relying too much on other people telling you the rules. Especially in informal settings like Stack Overflow, people have a tendency to simplify and misspeak, leading to misunderstandings — meanwhile, the official rules have been proofread many, many times over the years.

最终幸福 2024-09-08 00:39:06

是的 - 你已经基本回答了你自己的问题。

并非所有 API 都会保留,因为原始 NSString 可能会以某种方式发生更改 - 例如,一个 API 可能会收到 NSString 引用,但它实际上是一个 NSMutableString。

Objective C 的 Google 编码标准有一个关于此的部分,< a href="http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml?showone=Setters_copy_NSStrings#Setters_copy_NSStrings" rel="nofollow noreferrer">此处

Yes - you've pretty much answered your own question there.

Not all API's retain as the original NSString may change in some way - one API may receive an NSString reference, but it's actually an NSMutableString, for exapmle.

The Google coding standards for Objective C has a section about this, here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文