gpt4 book ai didi

objective-c - 监视特定文件夹的更改并检测 Cocoa 中哪些文件发生了更改

转载 作者:行者123 更新时间:2023-12-03 16:58:12 26 4
gpt4 key购买 nike

我正在尝试制作一个应用程序,它可以简单地监视特定文件夹的更改并输出更改的文件的路径。稍后它将对这些更改的文件进行一些处理。我将如何在本地 cocoa 中做到这一点?我已经尝试过以下列出的内容: http://developer.apple.com/library/mac/#featuredarticles/FileSystemEvents/_index.html

但我不知道如何有效地完成任务。

代码示例将不胜感激。

最佳答案

看看fs-notifier通过 Peter Hosey

@interface Notifier : NSObject {
NSArray *paths; //Actually just one.
FSEventStreamRef stream;
struct FSEventStreamContext context;
}

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

- (void) start;
- (void) stop;

@end
#import "Notifier.h"

@implementation Notifier

+ (id) notifierWithCallback:(FSEventStreamCallback)newCallback path:(NSString *)newPath {
return [[[self alloc] initWithCallback:newCallback path:newPath] autorelease];
}
- (id) initWithCallback:(FSEventStreamCallback)newCallback path:(NSString *)newPath {
if((self = [super init])) {
paths = [[NSArray arrayWithObject:newPath] retain];
context.version = 0L;
context.info = newPath;
context.retain = (CFAllocatorRetainCallBack)CFRetain;
context.release = (CFAllocatorReleaseCallBack)CFRelease;
context.copyDescription = (CFAllocatorCopyDescriptionCallBack)CFCopyDescription;

stream = FSEventStreamCreate(kCFAllocatorDefault, newCallback, &context, (CFArrayRef)paths, kFSEventStreamEventIdSinceNow, /*latency*/ 1.0, kFSEventStreamCreateFlagUseCFTypes);
if (!stream) {
NSLog(@"Could not create event stream for path %@", newPath);
[self release];
return nil;
}

FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
}
return self;
}

- (void) dealloc {
[self stop];
FSEventStreamUnscheduleFromRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFRelease(stream);
[super dealloc];
}

- (void) start {
FSEventStreamStart(stream);
}
- (void) stop {
FSEventStreamStop(stream);
}

@end

#import "Notifier.h"

static void gotEvent(ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[]);

int main (int argc, char **argv) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSArray *paths = [[NSProcessInfo processInfo] arguments];

NSMutableArray *streams = [NSMutableArray arrayWithCapacity:[paths count]];
for (NSString *path in paths) {
[streams addObject:[Notifier notifierWithCallback:gotEvent path:path]];
}

[streams makeObjectsPerformSelector:@selector(start)];
CFRunLoopRun();

[pool drain];
return EXIT_SUCCESS;
}

static void gotEvent(ConstFSEventStreamRef stream,
void *clientCallBackInfo,
size_t numEvents,
void *eventPathsVoidPointer,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[]
) {
NSArray *eventPaths = eventPathsVoidPointer;
NSString *streamName = clientCallBackInfo;
NSLog(@"%@: %@", streamName, [eventPaths objectAtIndex:0UL]);
}

关于objective-c - 监视特定文件夹的更改并检测 Cocoa 中哪些文件发生了更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13354561/

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