我创建了一个 NSCell 的子类并重写了 setObjectValue 函数来实现我的需要。除了存在泄漏问题外,一切都很好。NSCell 的 setObjectValue 函数似乎不会释放其原始对象。
我给类的对象是一个自定义对象,它符合 NSCopying 协议并实现了 copyWithZone 函数,如下所示
- (void)setObjectValue:(id < NSCopying >)object {
// I tried to use [[self objectValue] release]; here but the app crashed
[super setObjectValue:object];
//---- other iniatizlize here ---
}
.
- (id)copyWithZone:(NSZone *)zone {
MapComponent *newCopy = [[MapComponent allocWithZone:zone] initWithType:self.componentType];
newCopy.myImage = self.myImage;
return newCopy;
}
我在这里发现了同样的问题。没有答案,但可能更好地描述我的情况。