是否可以从C ++类方法调用目标C方法?我知道这已经得到了一定程度的回答,但是当我尝试使用目标C实例变量(指向self的指针)来调用方法时,我得到“使用未声明的标识符”时,似乎没有任何接受的答案对我有效。 。
@interface RTSPHandler : NSObject {
id thisObject;
}
执行力度:
-(int)startRTSP:(NSString *)url {
thisObject = self;
// start rtsp code
}
void DummySink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes,
struct timeval presentationTime, unsigned ) {
[thisObject receivedRTSPFrame:fReceiveBuffer];
}
-(void)receivedRTSPFrame:(NSMutableData* )data {
// decode frame..
}
错误:使用未声明的标识符'thisObject'
答案 0 :(得分:1)
尝试将thisObject
声明为静态变量,如下所示
static id thisObject;
@implementation RTSPHandler
//...
@end
<强>更新强>
确定。现在我看到我的答案很有趣。让我们完成任务并使解决方案更合适。
将有两个单独的类,具有独立的接口和实现部分。比喻名为OCObjectiveClass
(Objective-c类)和DummySink
(C ++类)的objective-c类。每个DummySink
实例必须具有OCObjectiveClass
对象作为c ++类成员。
这是OCObjectiveClass
的接口部分(&#34; .h&#34; -extension):
@interface OCObjectiveClass : NSObject
//...
- (void)receivedRTSPFrame:(void *)frame; // I don't know what is the frame's type and left it with a simple pointer
//...
@end
这是DummySink
的接口部分(&#34; .h&#34; -extension):
#import "OCObjectiveClass.h" // include objective-c class headers
class DummySink
{
OCObjectiveClass *delegate; // reference to some instance
//...
void AfterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes,struct timeval presentationTime, unsigned);
//...
}
AfterGettingFrame
函数实现必须在DummySink
类实现部分(不是&#34; .cpp&#34;扩展名,它必须是&#34; .mm&#34;才能与目标一起工作-c类和方法)。
void DummySink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes,
struct timeval presentationTime, unsigned ) {
[delegate receivedRTSPFrame:fReceiveBuffer];
}
不要忘记设置delegate
值。
- (void)someMethod
{
OCObjectiveClass *thisObject;
// initialize this object
DummySink sink;
sink.delegate=thisObject;
sink.DoWork();
}