我正在整理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个实例变量..

Objective-c相关问答推荐

在没有for循环的情况下获取另一个数组中每个对象的属性值数组

viewWillAppear,viewDidAppear 没有被调用,没有触发

当 UITextField 成为第一响应者时如何使 UIScrollView 自动滚动

通过指针算术访问数组值与 C 中的下标

Objective-c中release和dealloc的区别

将未知数量的行添加到静态单元UITableView

go 掉 UISearchBar 下的 1px 边框

无法编译连接:错误是什么意思?

iOS:将 URL 解析为段

在Objective-C中从类名的NSString创建对象

Objective-C 中的 alloc、init 和 new

UIBezierPath 减go 路径

从 UITabBarController 在当前上下文中呈现模态视图控制器后出现黑屏

从 iPhone 设备查找当前国家

如何逐行从 NSFileHandle 读取数据?

如何为 iPhone 6/7 自定义边到边图像指定尺寸?

如何在键盘上添加完成按钮?

使 UILabel 的文本加粗

ld:找不到文件:链接器命令失败,退出代码为 1

哪个是正确的,nil 或 NULL,来标记没有 Objective-C 块?