在FSEventStreamCreate内部声明回调函数 [英] Declaring callback function inside FSEventStreamCreate

查看:305
本文介绍了在FSEventStreamCreate内部声明回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几个使用FSEvent侦听文件系统中更改的示例.

There are a couple examples of using an FSEvent to listen for changes in the file system.

如何侦听文件系统更改MAC-kFSEventStreamCreateFlagWatchRoot

OS X Leopard上的FSEvent异常

使用FSEventStreamCreate创建事件时,它们似乎都通过了回调项.没有参数或任何东西,只是&feCallback.基本上,如果可以的话,似乎他们是在传递变量而不是函数.

When creating the event with FSEventStreamCreate they all seem to pass the callback item just fine. No parameters or anything, just &feCallback. Basically it seems as if they're passing a variable rather than a function, if that makes sense.

但是尝试执行此操作时出现Use of Undeclared identifier错误.有什么作用?

But I'm getting a Use of Undeclared identifier error when I try to do it. What gives?

 FSEventStreamRef stream = FSEventStreamCreate(NULL, 
                                                  &feCallback, // what does '&' mean? Why are no parameters passed?
                                                  &cntxt, 
                                                  pathsToWatch, 
                                                  kFSEventStreamEventIdSinceNow, 
                                                  1,
                                                  kFSEventStreamCreateFlagWatchRoot );

,然后具有回调函数:

static void feCallback(ConstFSEventStreamRef streamRef,
                       void* pClientCallBackInfo,
                       size_t numEvents,
                       void* pEventPaths,
                       const FSEventStreamEventFlags eventFlags[],
                       const FSEventStreamEventId eventIds[]) 
{
        NSLog(@"The file changed!"); 
}

我很喜欢一些示例代码来使开源帮助程序对象在这里工作: https ://bitbucket.org/boredzo/fs-notifier/overview

I'd love some sample code to get the open-source helper object here working: https://bitbucket.org/boredzo/fs-notifier/overview

但是同样的事情.它具有以下方法:

But same thing. It has the method:

- (id) initWithCallback:(FSEventStreamCallback)newCallback path:(NSString *)newPath;

,由于上述错误,我无法将其传递给newCallback.

and I can't pass it a newCallback because of the error described above.

推荐答案

使用FSEventStreamCreate创建事件时,它们似乎都通过了回调项.没有参数或其他任何内容,只需&feCallback.

When creating the event with FSEventStreamCreate they all seem to pass the callback item just fine. No parameters or anything, just &feCallback.

FSEventStreamRef stream = FSEventStreamCreate(NULL, 
                                                 &feCallback, // what does '&' mean? Why are no parameters passed?

&是运算符的地址",其结果是指向其操作数的指针.这里不是真的有必要.不管有没有&,不在函数调用中的函数名称总会得出指向该函数的指针.

The & is the "address of" operator, and evaluates to a pointer to its operand. It's not really necessary here; a function name not in a function call always evaluates to the pointer to the function, with or without &.

因此,它将指针传递给feCallback函数,这是FSEventStream对象将其回调的方式.

So this passes the pointer to the feCallback function, which is how the FSEventStream object will call it back.

如果可以的话,基本上看起来好像他们在传递变量而不是函数.

Basically it seems as if they're passing a variable rather than a function, if that makes sense.

这确实有道理,但是不,那是不正确的.他们正在传递函数本身.

That does make sense, but no, that's not correct. They are passing the function itself.

可以声明一个包含指向函数的指针的变量.如果feCallback是这样的变量,则feCallback&feCallback的含义不同:feCallback是该函数的指针(在feCallback变量内部),而&feCallback是该函数的指针变量.

It is possible to declare a variable that holds a pointer to a function. If feCallback were such a variable, then feCallback and &feCallback would mean different things: feCallback would be the pointer to the function (inside the feCallback variable), whereas &feCallback would be the pointer to the variable.

在您的情况下,以feCallback为函数,这两个表达式是等效的.任一种方式都会传递函数指针.

In your case, though, with feCallback being a function, the two expressions are equivalent; either way passes the function pointer.

但是,当我尝试使用它时,出现使用未声明的标识符"错误.有什么作用?

But I'm getting a Use of Undeclared identifier error when I try to do it. What gives?

您尚未声明该标识符(在本例中为该函数).

You haven't declared that identifier (in this case, that function).

,然后具有回调函数:

and then later have the callback function:

就是这个问题.在使用回调函数名称后,您已定义了该回调函数,但并未在函数之前中使用其名称声明该函数.您必须先声明该函数,然后才能使用它(或将其传递到任何地方).

That's the problem. You have the callback function defined after your use of its name, but you did not declare the function before using its name. You must declare the function before you can use it (or pass it anywhere).

原因是因为直到您告诉编译器,声明器才知道"feCallback"是什么.当您尝试在声明或定义"feCallback"之前引用它时,编译器不知道您在说什么.

The reason is because the compiler doesn't know what "feCallback" is until you tell it, which is what the declaration does. When you try to refer to "feCallback" before declaring or defining it, the compiler doesn't know what you're talking about.

另一种方法是将函数的定义上移.定义将视为其后所有内容的声明.不过,我将定义保留在原处,仅在文件顶部附近添加一个声明.

The other way would be to move the function's definition up. A definition counts as a declaration for everything that follows it. I'd leave the definition where it is, though, and just add a declaration near the top of the file.

无论哪种方式,当您将feCallback传递给FSEventStreamCreate时,编译器都会知道它是什么.

Either way, then the compiler will know what feCallback is when you pass it to FSEventStreamCreate.

这篇关于在FSEventStreamCreate内部声明回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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