在 Objective-C 中观察文件或文件夹 [英] Observe a File or Folder in Objective-C

查看:20
本文介绍了在 Objective-C 中观察文件或文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

收听文件夹或文件以查看是否已保存或是否添加了新文件的最佳方法是什么?

What is the best way to listen to a folder or file to see if it has been saved or if a new file has been added?

推荐答案

如果您只想查看目录但不处理单个文件的监视,FSEvents API 是理想的选择.Stu Connolly 为 FSEvents C API 提供了一个很棒的 Objective-C 包装器,它称为 SCEvents,您可以在此处获取:

The FSEvents API is ideal if you just want to watch directories but it doesn't handle the monitoring of individual files. Stu Connolly has a great Objective-C wrapper for the FSEvents C API, it's called SCEvents and you can get it here:

http://stuconnolly.com/blog/scevents-011/

FSEvents 的好处在于,您只需要观察一个文件夹,当该文件夹的子文件夹层次结构中的任何位置发生任何更改时,您都会收到通知.

The nice thing about FSEvents is that you just need to watch one folder and you will be notified of any changes that occur anywhere in the subfolder hierarchy of that folder.

如果您需要文件级通知,则需要使用 kqueue.Uli Kusterer 有一个很棒的 Objective-C 包装器:

If you need file-level notifications you will need to use kqueues. Uli Kusterer has a great Objective-C wrapper:

http://zathras.de/angelweb/sourcecode.htm#UKKQueue

这两种方法中的任何一种都比直接使用 C API 容易得多,因为 C API 没有特别好的文档,而且有点迟钝.

Either of these methods is a lot easier than wrangling with the C APIs directly, which are not particularly well documented and a bit obtuse.

如果您需要支持 Tiger,则需要使用 kqueues,因为 FSEvents API 在 10.4 中未正式提供.

If you need to support Tiger you'll need to use kqueues as the FSEvents API wasn't officially available in 10.4.

这篇关于在 Objective-C 中观察文件或文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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