我想创建一个简单的事件流,以便在目录中发生某些更改时监听事件。第一步是创建流,但我在使用 FSEventStreamCreate 函数创建时收到错误。谷歌搜索这个错误是没有用的,我不明白错误在哪里。
(CarbonCore.framework) FSEventStreamCreate: _FSEventStreamCreate: ERROR: (CFStringGetTypeID() != CFGetTypeID(cfStringRef)) (i = 0)
我的代码与苹果文档中的代码非常相似
无论如何,这是我的代码:
static void gotEvent(ConstFSEventStreamRef stream,
void *clientCallBackInfo,
size_t numEvents,
void *eventPathsVoidPointer,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[]
) {
NSLog(@"File Changed!");
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *fileInputPath = @"/tmp/mamma/ciao.txt";
FSEventStreamRef stream = [self eventStreamForFileAtPath: fileInputPath];
}
- (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];
}
CFStringRef fileInputDir = (CFStringRef)[fileInputPath stringByDeletingLastPathComponent];
CFArrayRef pathsToWatch = CFArrayCreate(NULL, (const void **) fileInputDir, 1, NULL);
void *callbackInfo = NULL;
CFAbsoluteTime latency = 3.0; /* Latency in seconds */
FSEventStreamRef stream = FSEventStreamCreate(
NULL,
&gotEvent,
callbackInfo,
pathsToWatch,
kFSEventStreamEventIdSinceNow,
latency,
kFSEventStreamEventFlagNone
);
return stream;
}