我有一个NSTimer
从类方法调用来控制NSProgressBar
。虽然我在使用NSTimer时会收到警告,但我假设编译器需要类名。
当我将班级名称MyProgressBar
代替NSTimer
时,警告似乎消失了。事情的实际情况是,幕后所有的一切都在崩溃,内存分配开始飙升。
问题是,这应该怎么做?
·H
@interface MyProgressBar : NSProgressIndicator {
double progressOffset;
NSTimer* animated;
}
@property (readwrite, retain) NSTimer* animated;
@property (readwrite) double progressOffset;
的.m
- (void)setDoubleValue:(double)value {
[super setDoubleValue:value];
if (![self isDisplayedWhenStopped] && value == [self maxValue]) {
[self stopAnimation:self];
}
}
- (NSTimer*)animated { // This is the line with the warning
return animated; // using MyProgressBar ends up creating a memory leak
}
- (void)setAnimated :(NSTimer *)value {
if (animated != value) {
[animated invalidate];
animated = value;
}
}
- (id)initWithFrame :(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
self.progressOffset = 0;
self.animated = nil;
}
return self;
}
警告:
Method is expected to return an instance of its class type 'MyProgressBar', but is declared to return 'NSTimer *'
Overridden method returns an instance of its class type
- > complete github project here that displays the warning
答案 0 :(得分:1)
应该将getter称为animated
,而不是animate
。
此外,您应该在所有实例变量(默认模式)中使用下划线并删除getter / setter,因为编译器将提供更好的实现(即setAnimated
看起来不正确,如果我重新收集使用手动引用计数)。 getter应返回autorelease
d对象。