Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Objective-C "self" from C method [duplicate]

Tags:

c

ios

objective-c

I have Objective-C class in my iOS project that implements Objective-C and C code in the same class. I changed the extension to .mm and this part goes well. Now I want to set a C method that will call the Objective-C method in the same class. The problem I get is when I am trying to call self from the C method. Here is the code :

void SetNotificationListeners(){
    [self fireSpeechRecognition];
}

the error is :

Use of undeclared identifier 'self'

how can I manage this?

like image 310
or azran Avatar asked Nov 24 '13 09:11

or azran


1 Answers

You have to pass the instance pointer to the C function:

void SetNotificationListeners(void *uData)
{
    MyClass *obj = (__bridge MyClass *)(uData);
    [obj fireSpeechRecognition];
}

- (void)myMethod
{
    // Call C function from Objective-C method:
    myFunction((__bridge void *)(self));
}

(The "brigde" stuff is needed only if you compile with ARC.)

like image 149
Martin R Avatar answered Oct 24 '22 18:10

Martin R