I just started learning targets, and have followed all the instructions to create my first console app which should print “Ouch!” every two seconds, but although it is looping it isn’t printing to the console and I can’t figure out why. Would someone mind telling me what I am doing wrong?
My .h file has the following:
#import <Foundation/Foundation.h>
@interface Logger : NSObject
- (void)sayOuch:(NSTimer *)t;
@end
My .m file has the following:
#import "Logger.h"
@implementation Logger
- (void)sayOuch:(NSTimer *)t
{
NSLog(@"Ouch!");
}
@end
And finally, my main.m file contains the following:
#import <Foundation/Foundation.h>
#import "Logger.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
Logger *logger = [[Logger alloc] init];
__unused NSTimer *timer = [NSTimer timerWithTimeInterval:2.0
target:logger
selector:@selector(sayOuch:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] run];
}
return 0;
}
The thing is, whenever I run the message “Ouch!” is not printing to my console. Thanks for the help in advance.
Change
with
So your code changed to working: