5

I am banging my head against an odd error after a move to 10.12/Sierra and Xcode 8.1:

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

The most minimal code (default settings of create a new project) to reproduce this is:

//  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);
    }];
}

The linking includes the (Core)Foundation classes and 'all_load'. Must be something totally trivial - but fail to what it is.

Any and all help appreciated.

Thanks,

Dw.

Dirk-Willem van Gulik
  • 7,566
  • 2
  • 35
  • 40

1 Answers1

31

+[NSTimer scheduledTimerWithTimeInterval:repeats:block:] is an iOS 10.0+ method. Are you maybe trying to run it on iOS 9.x?

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

Alessandro Mulloni
  • 1,011
  • 1
  • 9
  • 10
  • 1
    You can check if the new method exists with the following code and fallback to an older API if need be. if ([[NSTimer class] respondsToSelector:@selector(scheduledTimerWithTimeInterval:repeats:block:)]) { .. } – Awesomeness Feb 03 '17 at 18:38
  • I was wondering why I am facing this weird crash only on 9.x device. Thank you Alessandro https://developer.apple.com/documentation/foundation/nstimer/2091888-timerwithtimeinterval?language=objc if ([self.timer respondsToSelector:@selector(scheduledTimerWithTimeInterval:‌​repeats:block:)]) { // Code for iOS 10.0+ } else { // Code for IOS version lower than 10.0 } – Jitendra Nov 23 '17 at 13:27
  • 1
    This just cost me an expedited review. Normally XCode produces a warning if you include code only available in an iOS version newer than your target... but not this. – nickdnk Aug 31 '18 at 22:25