2015-09-16 60 views
0

我有一個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

回答

1

該吸氣劑應稱爲animated而不是animate

此外,您應該在所有實例變量(默認模式)中使用下劃線,並放棄getter/setter,因爲編譯器會提供更好的實現(即setAnimated看起來不正確,如果我記得使用手動引用數數)。吸氣劑應該返回一個autorelease d對象。

+0

是的,這是我的錯誤,我的意思是動畫。儘管如此,還是同樣的問題 - 謝謝。這樣做的原始方式確實像你提到的那樣釋放它,儘管因爲它現在是ARC,所以我認爲這是一個不行。 [這是原始方法](https://github.com/larcus94/LBProgressBar/blob/master/LBProgressBar/LBProgressBar.m#L44)。 –

+0

那麼我會放棄你的getter/setter實現;他們被打破了。 – trojanfoe

+0

@RoryZipher看到[這](http://stackoverflow.com/questions/4936261/retaining-repeating-nstimer-for-later-access)的答案。 – trojanfoe