So I am implementing an UI control, much like a textfield, but supposedly configurable so that I can give it the look and feel that I want.
To make a caret blink when the control receives focus I attach a NSTimer to the main thread’s run loop. Todo that I just have to call a class method from the NSTimer class and it creates and adds it to the run loop object.
To create a timer I have to instantiate it with a call to
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds invocation:(NSInvocation *)invocation repeats:(BOOL)repeats
Which takes me to the subject of instantiating an NSInvocation Object. For that we have the method
+ (NSInvocation *)invocationWithMethodSignature:(NSMethodSignature *)signature
To create a NSMethodSignature we need to call the method :
+ (NSMethodSignature *)signatureWithObjCTypes:(const char *)types
The apple docs say that “Returns an
NSMethodSignature object for the given Objective C method type string.”
What is an Objective C type string ? Its a character code for every primitive type in the Objective C language and can be consulted in the following link:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html So the rest is just as easy as RTFM. Edit : WAIT ! I just RTFM and indeed the above method of creating the method signature for the NSInvocation is platform dependent. Depending on the version of OS X you are on it may change. So we are better off using the rolling procedure :
NSInvocation* blinkMethod = [NSInvocation invocationWithMethodSignature: [self methodSignatureForSelector:@selector(blinkCaret)]];