Oblivious Sage
Oblivious Sage

Reputation: 3405

+[NSTimer timerWithTimeInterval:block:repeats:]: unrecognized selector sent to class

I'm trying to learn iDevice programming and learn to use a new 3rd-party library that will be used to interface with some other stuff for the project. The library, alas, is poorly documented, and I'm having trouble figuring out how the one example program works (the two leading theories from my team for how it works are "voodoo" and "ninja magic").

I've been slowly copying pieces that look like they're part of the core functionality (rather than part of all the bells and whistles that were tacked on to make the example app look professional) into a new project and trying to make them work. Right now I'm getting this error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSTimer timerWithTimeInterval:block:repeats:]: unrecognized selector sent to class 0x144e790'

I've already copied over everything from the original file that has anything to do with timers, reproduced here:

@property (nonatomic) NSTimeInterval currentTime;
...
@synthesize currentTime;

// lots of other stuff that has nothing to do with Timers

-(void)setCurrentTime:(NSTimeInterval)time {
   [self willChangeValueForKey:@"currentTime"];
   currentTime = time;
   [self didChangeValueForKey:@"currentTime"];
}

What selector is it looking for that I need to add?

EDIT:

At tc.'s request I went looking for that method in the library, and found this in a file on its own:

#import <Foundation/Foundation.h>

/**
 * A block based extension for NSTimer
 */

@interface NSTimer (EMAdditions)

/**
 * Allows you set a block for execution when the timer fires.
 * @param interval The time interval
 * @param block The block to execute
 * @param repeat A flag to indicate if the block should continuously repeat
 */
+(NSTimer *)timerWithTimeInterval:(NSTimeInterval)interval block:(void(^)(void))block repeats:(BOOL)repeat;

@end

I don't have access to the library's source code (it's just a big .a file), but shouldn't it have the implementation for this method? I tried doing a #import of the file that has this, but that didn't resolve the problem.

Upvotes: 0

Views: 4089

Answers (3)

Parag Bafna
Parag Bafna

Reputation: 22930

Add NSTimer category to your project. Here are the files (add NSTimer+Blocks.h and NSTimer+Blocks.m).

Upvotes: 1

Oblivious Sage
Oblivious Sage

Reputation: 3405

As tc. pointed out, this was an issue of the linker not including all of the NSTimer portion of the library.

The solution was to add the -all_load flag to the linker.

Upvotes: 1

TheWiseOne
TheWiseOne

Reputation: 101

Well, it appears from the error that NSTimer is looking for this selector [NSTimer timerWithTimeInterval:block:repeats:]

Looking in the documentation that is not even a thing, so is it possible that whatever library you are using added that function to NSTimer?

the only existing functions that look similar on NSTimer are: [NSTimer timerWithTimeInterval: invocation: repeats:] [timerWithTimeInterval:target:selector:userInfo:repeats:]

If you want the timer to call setCurrentTime and the timer is initialized in the same file that implements setCurrentTime, you could use the second one, set target to self, and set selector to @selector(setCurrentTime:)

Hope this helps

Upvotes: 0

Related Questions