How to pass objective-c function as a callback to C function?

前端 未结 2 1684
你的背包
你的背包 2021-01-03 09:41

I want to call a c function from objective-c and pass objective-c function as a callback

the problem is this function has a callback as parameter, so I have to pass

2条回答
  •  失恋的感觉
    2021-01-03 10:24

    Your chief problem is the first parameter to mg_start(), which is described in the declaration as const struct mg_callbacks *callbacks. You are trying pass a pointer to a function. (Actually you are trying to pass the result of a call to that function, which is even further from the mark.) That isn't what it says: it says a pointer to a struct (in particular, an mg_callbacks struct).

    The example code at https://github.com/valenok/mongoose/blob/master/examples/hello.c shows you how to configure this struct. You have to create the struct and put the pointer to the callback function inside it. Then you pass the address of that struct.

    Other problems with your code: your callback function itself is all wrong:

    - (void)serverstarted
    {
        NSLog(@"server started");
    }
    

    What's wanted here is a C function declared like this: int begin_request_handler(struct mg_connection *conn), that is, it takes as parameter a pointer to an mg_connection struct. Your serverstarted not only doesn't take that parameter, it isn't even a C function! It's an Objective-C method, a totally different animal. Your use of the term "Objective-C function" in your title and your question is misleading; C has functions, Objective-C has methods. No Objective-C is going to be used in the code you'll be writing here.

    What I suggest you do here is to copy the hello.c example slavishly at first. Then modify the content / names of things slowly and bit by bit to evolve it to your own code. Of course learning C would also help, but you can probably get by just by copying carefully.

提交回复
热议问题