Callback methods from C to Objective-C

前端 未结 4 1294
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 07:03

I have an Objective-C class. What I am doing is I am calling C functions in my objective-C class [ This C functions I implemented in one file , which is part in this sample ios

相关标签:
4条回答
  • 2021-01-21 07:07

    You can use blocks. Blocks are available to pure C and C++, a block is also always an Objective-C object.

    C++ Blocks Objects

    0 讨论(0)
  • 2021-01-21 07:09

    First of all Thanks to Ricardo Kloth , Now it's working for me.

    Here is my code ...

    Objective-C Code:

    static id staticObject = nil;
    
    @Implementation MyObjCclass
    
    init
    {
        ....
    
        staticObject = self;
    }
    
    // C fun
    void functionPointer() 
    { 
        [staticObject message]; // it's working 
    }
    
    -(void) message
    {
    
    }
    
    @end
    
    0 讨论(0)
  • 2021-01-21 07:12

    even in Blocks also you can't access instance variables right. Please check the following code snippet.

    // this is simple block code

    NSString* (^trimTheStr)(NSString*) = ^(NSString *str) {

    [self myInstanceMethods]; // This will show error right 
    
    NSString *result = nil;
    
    result = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    return result;
    

    };

    0 讨论(0)
  • 2021-01-21 07:20

    I ran into this problem aswell. I assume you are using PJSIP for your VoIP app.

    What you want to do is create a pointer to the Objective-C class you'd like to send messages to. You already figured you cannot call Objective-C functions from your C callback.

    static YourObjCClass *objCClassPtr

    @property (nonatomic, retain) YourObjCClass *class

    When initializing said class, have the static pointer point to the Objective C object's pointer. (pointer to pointer to object)

    objCClassPtr = class;

    You are now able to send messages to the Objective-C object using [objCClassPtr message] from your C function as if you would write [class message]

    You can point to self the same way.

    0 讨论(0)
提交回复
热议问题