我如何确保没有任何内容被自动释放?
我需要一种方法来创建在自定义类启动到调用发布函数的整个过程中都可以访问的变量。我需要保留一个 NSDate 和一个 NSString。
I need a way to create variables that are accessible for the entire time my custom class is initiated to when I call the release function. I need to retain a NSDate and a NSString.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
向变量发送
retain
消息。Send the variable a
retain
message.执行
[myObjectretain]
或@property(nonatomic,retain)MyClass *myObject;
Do
[myObject retain]
or@property(nonatomic, retain) MyClass *myObject;
Autorelease 只是稍后触发一条 -release 消息。如果您希望变量保留下来,请在分配它们时保留它们。即使它们是自动释放的,您的保留也会增加保留计数,因此它们不会被释放。请务必在您的 dealloc 中释放它们。
Autorelease just fires a -release message at a later time. If you want your variables to stick around, -retain them when they're assigned. Even if they're autoreleased, your retain will increment the retainCount, so they will not be dealloced. Just be sure to -release them in YOUR dealloc.
让您的自定义类在其头文件中保留每个变量的属性:
请确保在其实现文件中创建正确的 setter 和 getter,或使用
@synthesize
:Give your custom class retain properties for each variable in its header file:
Be sure to create proper setters and getters, or use
@synthesize
, in its implementation file: