在 FSEventStreamCreate 中声明回调函数

发布于 2024-12-04 09:07:08 字数 1885 浏览 3 评论 0原文

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

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

OS X Leopard 上的 FSEvents 怪异

使用 创建事件时FSEventStreamCreate 他们似乎都很好地传递了回调项。没有参数或任何东西,只有 &feCallback。基本上,他们似乎传递的是变量而不是函数,如果这有意义的话。

但当我尝试执行此操作时,出现使用未声明的标识符错误。什么给?

 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

但同样的事情。它具有方法:

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

由于错误,我无法向其传递 newCallback如上所述。

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

How to listen for file system changes MAC - kFSEventStreamCreateFlagWatchRoot

and

FSEvents weirdness on OS X Leopard

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.

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

and then later have the callback function:

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

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;

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

动听の歌 2024-12-11 09:07:08

当使用FSEventStreamCreate创建事件时,它们似乎都很好地传递了回调项。没有参数或任何东西,只有 &feCallback

FSEventStreamRef 流 = FSEventStreamCreate(NULL, 
                                                 &feCallback, // '&' 的作用是什么意思是?为什么没有传递参数?

& 是“地址”运算符,其计算结果是指向其操作数的指针。这里其实没有必要;不在函数调用中的函数名始终计算为指向该函数的指针,无论是否带有 &

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

基本上,它们似乎传递的是变量而不是函数,如果这有意义的话。

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

可以声明一个包含指向函数的指针的变量。如果 feCallback 是这样一个变量,那么 feCallback&feCallback 将意味着不同的事情:feCallback 将是指向函数的指针(在 feCallback 变量内),而 &feCallback 将是指向变量的指针。

不过,在您的情况下,由于 feCallback 是一个函数,这两个表达式是等效的;无论哪种方式都会传递函数指针。

但是当我尝试执行此操作时,出现“使用未声明的标识符”错误。给出了什么?

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

然后再有回调函数:

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

原因是编译器不知道“feCallback”是什么,直到你告诉它,这就是声明的作用。当您在声明或定义之前尝试引用“feCallback”时,编译器不知道您在说什么。

另一种方法是将函数的定义向上移动。定义算作其后所有内容的声明。不过,我会将定义保留在原来的位置,只需在文件顶部附近添加一个声明即可。

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

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

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.

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.

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文