2016-11-10 40 views
3

我後移動到10.12/Sierra和Xcode的8.1撞我的頭靠在一個奇數錯誤:瑣碎 「+ [的NSTimer scheduledTimerWithTimeInterval:重複:塊:]:無法識別選擇」 錯誤

+[NSTimer scheduledTimerWithTimeInterval:repeats:block:]: 
    unrecognized selector sent to class 0x7fff78f1fa88 

最最小代碼(創建一個新項目的默認設置)重現此:

// AppDelegate.m 
// 

#import "AppDelegate.h" 

@interface AppDelegate() 

@property (weak) IBOutlet NSWindow *window; 
@property (strong, nonatomic) NSTimer * timer; 
@end 

@implementation AppDelegate 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 
    self.timer = [NSTimer scheduledTimerWithTimeInterval:10 
          repeats:YES 
          block:^(NSTimer * _Nonnull timer) 
    { 
     NSLog(@"Ping from %@", timer); 
    }]; 
} 

鏈接包括(核心)基礎類和'all_load'。必須是完全微不足道的東西 - 但不能達到目的。

任何和所有的幫助表示讚賞。

謝謝,

Dw。

+0

不管它的價值,這些代碼不會崩潰,我(10.12.1時,Xcode 8.1)。 –

回答

24

+ [NSTimer scheduledTimerWithTimeInterval:repeats:block:]是一個iOS 10.0+方法。你可能試圖在iOS 9.x上運行它?

https://developer.apple.com/reference/foundation/nstimer/2091889-scheduledtimerwithtimeinterval?language=objc

+1

您可以使用以下代碼檢查新方法是否存在,並在需要時回退到較舊的API。如果([[NSTimer類] respondsToSelector:@selector(scheduledTimerWithTimeInterval:repeatats:block :)]){ } – Awesomeness

+0

賓果,它是正確的 –

+0

我想知道爲什麼我只在9.x設備上面對這種奇怪的崩潰。謝謝亞歷山德羅 https://developer.apple.com/documentation/foundation/nstimer/2091888-timerwithtimeinterval?language=objc 如果([self.timer respondsToSelector:@selector ]){ // Code for iOS 10.0+ } else { // IOS版本低於10.0的代碼 } – Jitendra

相關問題