iphone - Objective-C 中的实例变量是否默认设置为 nil?

标签 iphone objective-c cocoa cocoa-touch memory-management

我正在整理我的 iPhone 应用程序的一些内存问题,并且我一直在考虑一些基础知识。如果我设置了一个 ivar 并且在对象的生命周期内从未使用过它,那么当我在其上调用 dealloc 时,会导致问题吗?例如

@interface testClass {
    id myobject;
}
@property (nonatomic, retain) id myobject;
@end

@implementation testClass
@synthesize myobject;
- (id)init {
    ...
    // Do I have to set myobject to nil here?
    // So if myobject isn't used the dealloc call to nil
    // will be okay? Or can you release the variable without
    // having set every object to nil that you may may not use 
    ...
}

...

// Somewhere in the code, myobject may be set to
// an instance of an object via self.myobject = [AnObject grabAnObject]
// but the object may be left alone

...

- (void)dealloc {
    [myobject release];
    [super dealloc];
}
@end

最佳答案

实例变量are initialized to 0在初始化程序运行之前..

关于iphone - Objective-C 中的实例变量是否默认设置为 nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1786781/

相关文章:

iphone - 根据容器以编程方式调整 UIViewController 的大小

ios - 转到 UITabController 中的不同选项卡

iphone - 如果被包装的文本中有一个长字符串,则 sizeWithFont 不会为 UITextView 提供正确的高度

cocoa - 为什么 kCIAttribute(Max|Min) 和 kCIAttributeSlider(Max|Min) 有时会有不同的值

javascript - Safari IOS - 输入不会失去对外部点击的关注(模糊不触发)

iphone - 将 subview 发送到后面

ios - 无法将 Sprite 添加到场景中

ios - 错误消息 'Expected ; after top level declarator ' 'Unknown type name import' 带有在 Objective-C 中使用 Swift 的桥

cocoa - 如何自定义 NSTokenField 就像 mac 邮件中的 "To/CC"字段一样?

cocoa - 如何移动 NSTextView 开头的插入点?