布尔值的Objective-C的dealloc [英] Objective-c dealloc of boolean value
问题描述
我将如何的dealloc
一个布尔值?
Deallocing它下面这种方式给了我一个警告:不兼容的指向整数转换分配给从布尔(又名符号字符')'无效*'
Deallocing it this way below gives me a warning: Incompatible pointer to integer conversion assigning to 'BOOL' (aka 'signed char') from 'void *'
- (void)dealloc {
self.booleanVar = nil;
[super dealloc];
}
也许我应该澄清,这是从NSObject的继承了一个简单的类。
Perhaps I should clarify, this is from a simple class inherited from NSObject.
我使用在可可触摸类看self.var =零格局。比方说,如果这是一个NSString *代替,我应该使用self.var =零,或在的dealloc
法[VAR发布]我有点困惑在这里。
I'm using the self.var = nil pattern that you see in Cocoa Touch classes. Let's say if it was an NSString* instead should I use self.var = nil or [var release] in the dealloc
method? I'm a little confused here.
推荐答案
您不必这样做。它的不可以的对象。这也解释了警告,因为你要分配一个零指针(这是一个对象的基本NULL)向非对象。
You don't need to do it. It is not an object. This also explains the warning, as you're trying to assign a nil pointer (that's a NULL for objects basically) to a non-object.
关于你的第二个问题,是的。你可以把原始变量作为是对象的一部分,所以当它被释放的增值经销商将不再存在。
Regarding your second question, yes. You can think of primitive variables as being part of the object, so when it's deallocated the vars will not exist anymore.
但是,当你有一个的NSString *
中的对象,它只是一个指向另一个对象。如果你的dealloc前,指针将被删除,而不是的NSString
。没有人会指向它,它是一种失去了在空气中,占用的内存。因此,删除指针之前,如果你不需要对象了,你把它释放的消息。这是在dealloc方法做的,因为它叫删除和对象,因此也删除已经没有用了其他所有对象的好地方。
But when you have a NSString *
in an object, it's just a pointer to another object. If you dealloc the former, the pointer will be deleted, not the NSString
. No one might point to it, it's kind of lost in the air, occupying memory. So, before deleting the pointer, if you won't need the object anymore, you send it a release message. That's done in the dealloc method, since it's called to "delete" and object and thus is a good place to delete also every other object that has no use anymore.
这篇关于布尔值的Objective-C的dealloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!