gpt4 book ai didi

objective-c - 使用 FSEventStreamCreate 创建的流为零

转载 作者:行者123 更新时间:2023-12-04 06:11:36 25 4
gpt4 key购买 nike

我想创建一个简单的事件流,以便在目录中发生某些更改时监听事件。第一步是创建流,但我在使用 FSEventStreamCreate 函数创建时收到错误。谷歌搜索这个错误没用,我不明白错误在哪里。

(CarbonCore.framework) FSEventStreamCreate: _FSEventStreamCreate: ERROR: (CFStringGetTypeID() != CFGetTypeID(cfStringRef)) (i = 0)

我的代码与 apple documentation 中的代码非常相似。

无论如何,这是我的代码:
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;
}

最佳答案

试试这个代码:

- (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 = (void *)self;

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

NSTimeInterval latency = 3.0;

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

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

return stream;

}

也看看这个链接: Monitoring File Changes with the File System Events API .它向您展示了如何监控 Cocoa 应用程序中的文件更改。

关于objective-c - 使用 FSEventStreamCreate 创建的流为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7714385/

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