gpt4 book ai didi

objective-c - FSEvents-检测文件夹上的事件类型

转载 作者:行者123 更新时间:2023-12-03 17:52:22 28 4
gpt4 key购买 nike

关于如何检测回调方法中文件夹中引发的文件夹事件(FSEvent)的类型(下面代码中的gotEvent方法)有什么想法吗?例如:文件已重命名、文件已创建?我只想对“文件重命名”、“文件创建”进行一些操作。想要忽略其他事件。

我有以下实现 -

- (FSEventStreamRef) eventStreamForFileAtPath: (NSString *) fileInputPath  {

if (![[NSFileManager defaultManager] fileExistsAtPath:fileInputPath]) {
@throw [NSException exceptionWithName:@"FileNotFoundException"
reason:@"There is not file at path specified in fileInputPath"
userInfo:nil];
}

NSString *fileInputDir = [fileInputPath stringByDeletingLastPathComponent];

NSArray *pathsToWatch = [NSArray arrayWithObjects:fileInputDir, nil];

void *appPointer = (__bridge void *)self;

FSEventStreamContext context = {0, appPointer, NULL, NULL, NULL};

NSTimeInterval latency = 3.0;

FSEventStreamRef stream = FSEventStreamCreate(NULL,
&gotEvent,
&context,
(__bridge CFArrayRef) pathsToWatch,
kFSEventStreamEventIdSinceNow,
(CFAbsoluteTime) latency,
kFSEventStreamCreateFlagUseCFTypes
);

FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
FSEventStreamStart(stream);

return stream;

}

static void gotEvent(ConstFSEventStreamRef stream,
void *clientCallBackInfo,
size_t numEvents,
void *eventPathsVoidPointer,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[]
) {

NSLog(@"File Changed!");
}

最佳答案

根据AppleFSEventStreamEventFlags应该指示发生了什么。 :

kFSEventStreamEventFlagItemCreated = 0x00000100,
kFSEventStreamEventFlagItemRemoved = 0x00000200,
kFSEventStreamEventFlagItemRenamed = 0x00000800,
kFSEventStreamEventFlagItemModified = 0x00001000,

评估设置的标志应该完全符合您的要求。

关于objective-c - FSEvents-检测文件夹上的事件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22088973/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com