【发布时间】:2015-09-16 17:07:08
【问题描述】:
我有一个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
【问题讨论】:
标签: objective-c xcode macos class nstimer