Objective-C / UIButton / SetTitle
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非另有说明,否则您可以依靠框架类来保留它们所需的内容。这是 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.
是的 - 你已经基本回答了你自己的问题。
并非所有 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