2013-04-26 56 views
0

我在實現由專用框架中的函數使用的對象的實現中實現了C回調,因此我無法控制傳遞給回調函數的參數。我想從回調中使用self。謝謝。如何從C函數中獲取對象的當前實例

編輯:

MyObject.h/.M

#import "PrivateFramework.h" 
@interface MyObject : NSObject  

-(void) start; 

@end 

void callback(void* arg1, int arg2); 
void callback(void* arg1, int arg2) { 

/* 
    here I would like to use self (current instance of the object) but 
    I can't pass a pointer to the instance in the callback since I don't 
    control what is passed to it. 
*/ 



@implementation MyObject 

-(void)start { 

    // this function is inside a private framework. 
    PrivateFunction(&callback); 
} 
@end 
+0

這很混亂。請顯示一些代碼。 – 2013-04-26 16:22:08

+0

不好意思,但這是不可理解的。 – 2013-04-26 16:23:56

+0

@ H2CO3我添加了代碼。 – b1onic 2013-04-26 16:37:41

回答

1

兩個選擇:

  1. 使用單管理過程
  2. 添加一個 '類' 變量(文件級靜態)來保存請求實例

    static MyObject *callbackResponder = nil;

這將您#imports和@implementation之間可能走在.m文件。

+1

Objective-C中沒有類變量。 – 2013-04-26 17:44:08

+1

更新了澄清濫用術語。 – Wain 2013-04-26 17:55:19

+0

謝謝!現在我明白你的意思了。 – 2013-04-26 18:55:26

0

回調的概念是創建塊的原因。我會研究塊編程指南。這不是C風格的回調,但它有類似的用法。

相關問題