1

I have a c++ class in my objective-c application (TC.h and TC.mm) and it has two methods as follows.

//TC.h
class TC 
{
public:
  void SetupPollTimer();
  void Timeout();
}

//TC.mm
void TC::Timeout() 
{
    //Inside timer handler

}
void TC::SetupPollTimer() 
{
    //Setup Timer
    //Want to use NSTimer here..

}

Basically when I call SetupPollTimer(), I want to start a NSTimer, with NSInvocation so that it repeatedly calls Timeout method. First off all, is this even possible. If so, I would really appreciate any thoughts or guidance regarding the same.

Sharath
  • 969
  • 9
  • 30

2 Answers2

2

In this case, you would just use the CoreFoundation equivalent; CFRunLoopTimer.

Using an NSTimer instead would often mean you need a binding objc object type to use as the parameter for your timer callbacks.

Doing that, you would set TC's this to the timer callback's context info. Then you would be able to access your TC instance in the callback.

justin
  • 104,054
  • 14
  • 179
  • 226
0

Take a look at this post:

Use C++ with Cocoa Instead of Objective-C?

Apparently, you can't write Cocoa-Applications entirely in C++, but there are ways to share code between C++ and Objective C.

EDIT

Ok, another option would be to add the timer to the class that handles that CPP file and fire the CPP method whenever the NSTimer fires in the handler-file.

I found a few techniques to use C++ instances in Objective C, but not the other way around.

Community
  • 1
  • 1
IluTov
  • 6,807
  • 6
  • 41
  • 103
  • This is a Cocoa app i.e. with appdelegates, objective-c interfaces/implementations etc. One of the classes HAS to be in C++. This CPP class needs to have a NSTimer running and call its own method...similar to the question I have posted. Basically, I am guessing you can pass the function pointer of the c++ method to NSInvocation, thus making it the selector, but not getting any concrete examples to use.. – Sharath Oct 22 '12 at 12:24