0

I have something like this:

// main.m
@interface MYView : UIView { @public int counter; } @end;
@implementation MYView 
- (void)drawRect:(CGRect)rect {
}
@end

now I would like to remove drawRect method (or any other method) from implementation here, and move it to C++ file utility.cpp.

The reason why I need this, is that I must use some C++ library, that will not compile in Objective-C++. And I need to use that library intensively so that just switching back and forth is not convenient.

I know I can simly do this:

- (void)drawRect:(CGRect)rect {
    call_cpp_function(rect);
}

but maybe I can just completely define the method in c++? Something like this:

void __magic_declaration__ MYView::drawRect(CPPTYPE(CGRect))
// note, this line is completely my own imagination, this is just to illustrate what I want.
exebook
  • 32,014
  • 33
  • 141
  • 226

2 Answers2

3

It is possible, if not exactly what you'd call pleasant. See this answer in which a view is constructed and drawn in pure C.

Community
  • 1
  • 1
Simon
  • 25,468
  • 44
  • 152
  • 266
2

You can't do this. The closest you can get is calling C++ code from an ObjC method as you mentioned.

Technically you might be able to hack something up that adds the method dynamically using the ObjC runtime API, but that will be much clunkier, harder, and slower.

Catfish_Man
  • 41,261
  • 11
  • 67
  • 84