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

我正在整理我的 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在初始化程序运行之前..

https://stackoverflow.com/questions/1786781/

相关文章:

objective-c - 为什么不鼓励使用伞式框架?

ios - UIImagePickerController 相机 View 在 iOS 8 上奇怪地

ios - 如何在 iOS 中使用 'Container View'?

ios - 带有 AVPlayer 的多个视频

iphone - Objective-C 实现文件中方法名后的分号

objective-c - 使用 JSON (iOS) 的 Cocoa 错误 3840

objective-c - 在子类中覆盖初始化

objective-c - 处理由于 iOS 通讯录 API 中的链接卡导致的重复联系人

ios - 切换隐私设置将杀死应用程序

iphone - Objective-C - 将 float 转换为字符串的其他方法