默认情况下,Objective-C中的实例变量设置为nil? [英] Are instance variables set to nil by default in Objective-C?

查看:178
本文介绍了默认情况下,Objective-C中的实例变量设置为nil?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整理我的iPhone应用程序的一些内存问题,我刚刚考虑一些基础。如果我设置一个ivar,从来没有最终使用它在我的对象的生命周期,当我调用dealloc它,将导致一个问题?例如

I'm sorting out some memory issues with my iPhone app and I've just been thinking about some basics. If I setup an ivar and never end up using it in the lifespan of my object, when I call dealloc on it, will that cause a problem? E.g.

@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


推荐答案

实例变量初始化为0

这篇关于默认情况下,Objective-C中的实例变量设置为nil?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆