I expect that code to display “New Version” at every 3 seconds but it doesn’t.
Car.h
#import <Foundation/Foundation.h>
@interface Car : NSObject
-(void)displayVersion;
@end
Car.m
#import "Car.h"
@implementation Car
-(void)displayVersion
{
NSLog(@"New version");
}
@end
main.c
#import <Foundation/Foundation.h>
#import "Car.h"
int main (int argc, const char * argv[])
{
@autoreleasepool
{
Car *ford = [[Car alloc]init];
[NSTimer scheduledTimerWithTimeInterval:3
target:ford
selector:@selector(displayVersion)
userInfo:nil
repeats:YES];
}
return 0;
}
What is wrong here ?
PS: I hate that “Your post does not have much context to explain the code sections; please explain your scenario more clearly”
I think the code is the best explanation of the problem !
NSTimer relies on a running NSRunLoop to function. Your program terminates immediately after you create and schedule the timer, and an NSRunLoop is never set up anyway. Typically, in a Cocoa app, the call to
NSApplicationMain()inmain()sets up up the main run loop and begins “spinning” it. You should create a new project using the default Cocoa app template in Xcode, and create your timer in the NSApplicationDelegate’s-(void)applicationDidFinishLaunching:method.