如何通过Objective-C的函数作为回调的C函数? [英] How to pass objective-c function as a callback to C function?

查看:189
本文介绍了如何通过Objective-C的函数作为回调的C函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Objective-C的调用C函数和传递的Objective-C函数作为回调

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

问题是这个函数有一个回调的参数,所以我要回传给了Objective-C的函数调用C函数

the problem is this function has a callback as parameter, so I have to pass objective-c function as a call back to c function

下面是C函数的头部

        struct mg_context *mg_start(const struct mg_callbacks *callbacks,
                        void *user_data,
                        const char **configuration_options);

这里就是我试图把它叫做

here is where I try to call it

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


- (IBAction)startserver:(id)sender {
   NSLog(@"server should start");
   const char *options[] =
   {
       "document_root", "www",
       "listening_ports", "8080",
        NULL
   };
   mg_start(serverstarted(), NULL, options);
  }

我曾尝试多种方法来做到这一点,并在网上搜索,只得到一个线索如何做到这一点但不是运气

I have tried several ways to do it and searched the web to just get a clue how to do it but with not luck

这里的图书馆,我incuding在我的code

here is the library I am incuding in my code

https://github.com/valenok/mongoose

推荐答案

您主要的问题是第一个参数 mg_start(),它在声明中描述 const的结构mg_callbacks *回调。您正在尝试一个指针传递给函数。 (其实你正在试图通过该功能,这是进一步从标记调用的结果),这是不是这样说的:它说一个指针的结构的(特别是,一个 mg_callbacks 结构)。

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).

这个例子code。在<一个href=\"https://github.com/valenok/mongoose/blob/master/examples/hello.c\">https://github.com/valenok/mongoose/blob/master/examples/hello.c向您展示如何配置这个结构。你必须创建结构,并把指针里面的回调函数。然后,你传递结构的地址。

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.

其他问题,您的code:你的回调函数本身都是错误的:

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

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

这是怎么想是一个C函数声明如下: INT begin_request_handler(结构mg_connection * conn)的,也就是说,它需要作为参数,一个指向 mg_connection 结构。你的 serverstarted 不但不采取参数,它甚至不是一个C函数!这是一个Objective-C的方法,一个完全不同的动物。您使用在您的标题术语Objective-C的功能和你的问题是误导性的; C有函数,Objective-C中有方法。没有Objective-C的会在code,你会在这里写被使用。

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.

我建议您做些什么这里是复制的hello.c 例如亦步亦趋地的在第一。然后,通过修改一点东西慢慢位内容/名称将其发展为自己code。当然,学习C也将有所帮助,但你可以通过只可能得到通过仔细复制。

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.

这篇关于如何通过Objective-C的函数作为回调的C函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆